Go: Why os.Getwd () sometimes fails with EOF

package main import "os" import "fmt" func main() { _, err := os.Getwd() if err != nil { fmt.Println(err) } } 

This sometimes prints an EOF error. Does anyone know why? Some kind of os.Chdir usually happens, but this is not an error.

 GOARCH="amd64" GOBIN="" GOCHAR="6" GOEXE="" GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOOS="darwin" go version go1.0.3 
+4
source share
1 answer

This is a bug in os.Getwd that causes it to return EOF as an error when the working directory no longer exists, on platforms that do not support syscall getwd . Here is a recurring test case of OS X.

 package main import "os" import "fmt" const DIR = "/tmp/somedir" func main() { os.Remove(DIR) if err := os.Mkdir(DIR, 0755); err != nil { fmt.Println(err) return } if err := os.Chdir(DIR); err != nil { fmt.Println(err) return } if err := os.Remove(DIR); err != nil { fmt.Println(err) return } wd, err := os.Getwd() fmt.Println("err:", err) fmt.Println("wd:", wd) } 

Output:

 err: EOF wd: 
+1
source

All Articles