Go Work Environment
Explore how to establish a Go work environment by organizing your workspace with src, bin, and pkg directories, setting GOPATH and PATH variables. Understand Go runtime essentials, environment customization, and cross-compiling to prepare for developing and deploying Go applications efficiently.
Workspace #
A folder structure with two directories at its root is typically is called a workspace. A typical Golang project keeps all its code in a single workspace. The workspace of Golang is:
- src, a folder that contains Go source code files
- bin, a folder that contains executable binaries (also called commands)
However, if you need external packages, there will also be a folder called pkg to accommodate these.
GOPATH variable
The workspace root location cannot be the same as the Go install location, and it is specified by the GOPATH environment variable. It is by default a go subfolder in the home directory, so $HOME/go on Unix or C:\Users\username\go on Windows. Add the bin folder of your workspace to the PATH variable so that your application binaries are automatically found. For example on Linux add the following to your .profile:
export GOPATH=$HOME/go # this is the default
export PATH=$PATH:$GOPATH/bin
...