Golang Packages

Jimmy (xiaoke) Shen
3 min readAug 25, 2020

--

Workspace[1]

Let’s discuss how to organize code in the Workspace before discussing Go packages. In Go, programs are kept in a directory hierarchy that is called a workspace. A workspace is simply a root directory of your Go applications. A workspace contains three subdirectories at its root:

src — This directory contains source files organized as packages. You will write your Go applications inside this directory.

pkg — This directory contains Go package objects.

bin — This directory contains executable programs.

You have to specify the location of the workspace before developing Go programs. The GOPATH environment variable is used for specifying the location of Go workspaces. From [1]

You can check your GOPATH by

jimmy@jimmys-MacBook-Pro sources % echo $GOPATH/Users/jimmy/go

If GOPATH is not set up, you can set it up by running the following commands in the ~/.zshrc file (for MAC users)

export GOPATH='/Users/jimmy/go'export PATH="$GOPATH/bin:$PATH"

Packages

In Go, source files are organized into system directories called packages, which enable code reusability across the Go applications. The naming convention for Go package is to use the name of the system directory where we are putting our Go source files. Within a single folder, the package name will be same for the all source files which belong to that directory. We develop our Go programs in the $GOPATH directory, where we organize source code files into directories as packages. In Go packages, all identifiers will be exported to other packages if the first letter of the identifier name starts with an uppercase letter. The functions and types will not be exported to other packages if we start with a lowercase letter for the identifier name.

Go’s standard library comes with lot of useful packages which can be used for building real-world applications. For example, the standard library provides a “net/http” package which can be used for building web applications and web services. The packages from the standard library are available at the “pkg” subdirectory of the GOROOT directory. When you install Go, an environment variable GOROOT will be automatically added to your system for specifying the Go installer directory. The Go developer community is very enthusiastic for developing third-party Go packages. When you develop Go applications, you can leverage these third-party Go packages. From [1]

As suggested from[2], we don’t need to set up the GOROOT. You can use GO without setting up the GOROOT environment variable.

jimmy@jimmys-MacBook-Pro ~ % echo $GOROOTjimmy@jimmys-MacBook-Pro ~ % go env GOROOT/usr/local/go

Import Packages

The keyword “import” is used for importing a package into other packages.When you import packages, the Go compiler will look on the locations specified by the environment variable GOROOT and GOPATH. Packages from the standard library are available in the GOROOT location. The packages that are created by yourself, and third-party packages which you have imported, are available in the GOPATH location. From [1]

Packages search order

As suggested from [3], the searching order is

The go compiler (e.g., when running go build or go install) searches for packages in the following order:

  • vendor tree (if it exists)
  • GOROOT
  • GOPATH (from left to right)

You can test it by building an unexisting package, it will show you the searching order.

jimmy@jimmys-MacBook-Pro ~ % go build jimmycan't load package: package jimmy: cannot find package "jimmy" in any of:/usr/local/go/src/jimmy (from $GOROOT)/Users/jimmy/go/src/jimmy (from $GOPATH)

Reference

[1]https://thenewstack.io/understanding-golang-packages/

[2]https://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really#:~:text=TL%3BDR,still%20need%20to%20set%20%24GOPATH%20.

[3]https://www.reddit.com/r/golang/comments/84slcb/golang_import_lookup_order/

--

--

No responses yet