Take a look at this shell session, where I build a simple hello world program in Go.
$ cd ~/lab/hello/
$ ls
hello.go
$ cat hello.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world
")
}
$ go build
$ ./hello
hello, world
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.7 (jessie)
Release: 8.7
Codename: jessie
Here is what I don't understand. The tutorial at https://golang.org/doc/install#testing says that I should place my hello.go
file at ~/go/src/hello. But I am not following this. How is my program compiling then? If my program is compiling fine in this manner, why does the documentation say that I should keep my source code at ~/go/src or $GOPATH/src when it does not seem to matter?
Is there a scenario where it is really necessary to place the source code at $GOPATH/src?