Hands-On System Programming with Go
上QQ阅读APP看书,第一时间看更新

Install

In Go, there are different commands to build packages and applications. The first one is go install, followed by a path or a package name, which creates a compiled version of the packages in the pkg directory inside $GOPATH.

All the compiled packages are organized by the operating system and architecture, which are stored in the $GOOS and $GOARCH environment variables. These settings are visible by using the go env command, together with other information, like compilation flags:

$ go env
GOARCH="amd64"
...
GOOS="linux"
GOPATH="/home/user/go"
...
GOROOT="/usr/lib/go-1.12"
...

For the current architecture and operating system, all the compiled packages will be placed in the $GOOS_$GOARCH subdirectory:

$ ls /home/user/go/pkg/
linux_amd64

If the package name is main and it contains a main function, the command will produce an executable binary file, which will be stored in $GOPATH/bin. If a package is already installed and the source file didn't change, it will not be compiled again, which will speed up build time significantly after the first compile.