Although the standard Go library is very rich, there are times that you will need to download external Go packages in order to use their functionality. This section will teach you how to download an external Go package and where it will be placed on your UNIX machine.
Look at the following naive Go program that is saved as getPackage.go:
package main import ( "fmt" "github.com/mactsouk/go/simpleGitHub" ) func main() { fmt.Println(simpleGitHub.AddTwo(5, 6)) }
This program uses an external package because one of the import commands uses an internet address. In this case, the external package is called simpleGitHub and is located at github.com/mactsouk/go/simpleGitHub.
If you try to execute getPackage.go right away, you will be disappointed:
$ go run getPackage.go getPackage.go:5:2: cannot find package "github.com/mactsouk/go/simpleGitHub" in any of: /usr/local/Cellar/go/1.9.1/libexec/src/github.com/mactsouk/go/
simpleGitHub (from $GOROOT) /Users/mtsouk/go/src/github.com/mactsouk/go/simpleGitHub (from $GOPATH)
So, you will need to get the missing package on your computer. In order to download it, you will need to execute the following command:
$ go get -v github.com/mactsouk/go/simpleGitHub github.com/mactsouk/go (download) github.com/mactsouk/go/simpleGitHub
After that, you can find the downloaded files at the following directory:
$ ls -l ~/go/src/github.com/mactsouk/go/simpleGitHub/ total 8 -rw-r--r-- 1 mtsouk staff 66 Oct 17 21:47 simpleGitHub.go
However, the go get command also compiles the package. The relevant files can be found at the following place:
$ ls -l ~/go/pkg/darwin_amd64/github.com/mactsouk/go/simpleGitHub.a -rw-r--r-- 1 mtsouk staff 1050 Oct 17 21:47 /Users/mtsouk/go/pkg/darwin_amd64/github.com/mactsouk/go/simpleGitHub.a
You are now ready to execute getPackage.go without any problems:
$ go run getPackage.go 11
You can delete the intermediate files of a downloaded Go package as follows:
$ go clean -i -v -x github.com/mactsouk/go/simpleGitHub cd /Users/mtsouk/go/src/github.com/mactsouk/go/simpleGitHub rm -f simpleGitHub.test simpleGitHub.test.exe rm -f /Users/mtsouk/go/pkg/darwin_amd64/github.com/mactsouk/go/
simpleGitHub.a
Similarly, you can delete an entire Go package you have downloaded locally using the rm(1) UNIX command to delete its Go source after using go clean:
$ go clean -i -v -x github.com/mactsouk/go/simpleGitHub $ rm -rf ~/go/src/github.com/mactsouk/go/simpleGitHub
After executing the former commands, you will need to download the Go package again.