Standard library / os

os.Getwd()

Returns an absolute pathname for the current working directory; more than one pathname may refer to that directory. Example uses a disposable temporary workspace. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func Getwd() (dir string, err error)

Return Value

(dir string, err error)

Returns an absolute pathname for the current working directory; more than one pathname may refer to that directory.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "path/filepath")
func check(e error) {  if e != nil {    panic(e)  }}func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()(); p, e := os.Getwd(); check(e); fmt.Println(filepath.IsAbs(p)) }
Go Reference