So is the value of os.Args[0] correct here?
Why am I getting a temp dir when running via go run?
$ go version
go version go1.6.2 darwin/amd64
$ pwd
/Users/hare/go/src/testgetwd
$ cat testgetwd.go
package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
fmt.Printf("os.Args[0]: %s\n", os.Args[0])
fmt.Printf("PWD: %s\n", os.Getenv("PWD"))
wd, _ := os.Getwd()
fmt.Printf("Getwd(): %s\n", wd)
ap, _ := filepath.Abs(filepath.Dir(os.Args[0]))
fmt.Printf("filepath.Dir(os.Args[0]): %s\n", ap)
}
$ go build testgetwd.go
$ ./testgetwd
os.Args[0]: ./testgetwd
PWD: /Users/hare/go/src/testgetwd
Getwd(): /Users/hare/go/src/testgetwd
filepath.Dir(os.Args[0]): /Users/hare/go/src/testgetwd
$ go run ./testgetwd.go
os.Args[0]: /var/folders/bb/ywkhzdf517ngn683g3c5l50c0000gn/T/go-build525038303/command-line-arguments/_obj/exe/testgetwd
PWD: /Users/hare/go/src/testgetwd
Getwd(): /Users/hare/go/src/testgetwd
filepath.Dir(os.Args[0]): /var/folders/bb/ywkhzdf517ngn683g3c5l50c0000gn/T/go-build525038303/command-line-arguments/_obj/exe
Output on Linux seems has similar issue:
$ ./testgetwd
os.Args[0]: ./testgetwd
PWD: /home/jh9/go/src/testgetwd
Getwd(): /home/jh9/go/src/testgetwd
filepath.Dir(os.Args[0]): /home/jh9/go/src/testgetwd
$ go run testgetwd.go
os.Args[0]: /tmp/go-build149397783/command-line-arguments/_obj/exe/testgetwd
PWD: /home/jh9/go/src/testgetwd
Getwd(): /home/jh9/go/src/testgetwd
filepath.Dir(os.Args[0]): /tmp/go-build149397783/command-line-arguments/_obj/exe
The docs are a bit sparse!
var Args []string
Args hold the command-line arguments, starting with the program name.
Note: I've got another app where Getwd() returns the temp path also (haven't figured out what's different with that one yet).
Anyone able to enlighten me on this?
What should I be using to reliably get the working directory?
Anyone able to enlighten me on this?
What should I be using to reliably get the working directory?