Go Workspace
Before introduction of Go Modules, all the work on Go projects were happening in Go Workspace. Right now it's not being used for development, but only for storing cached version of libraries and binary executives.
This section become outdated.
Go workspace
The $GOPATH
specify location of go workspace. Right after installing Go binary set your $GOPATH
variable, which most of the times, is set to $HOME/go
Go workspace layout:
src (outdated)
src (outdated)
Represents the directory where all the projects' source code and third party packages will live. So when making a reference to a third party package or one of your local packages, it will reference src
directory.
The source for a package with the import path "X/Y/Z"
lives in the directory $GOPATH/src/X/Y/Z
bin
bin
Represents the location where all Go binaries are stored. Usually when downloading a package using go get
or when we manually run go install
inside the location where we have a main package, in both cases these operations will result in creating a binary and storing it inside $GOPATH/bin
directory which is also represented by $GOBIN
environment variable.
The binary for a command whose source is in $GOPATH/src/A/B
lives in $GOPATH/bin/B
Also if $GOBIN
variable is included in path, you can run any compiled binary by simply typing their name.
So a good practice after installing Go binary is to include $GOBIN
inside $PATH
variable so, every binary is easily accessible.
By simply placing this inside your .profile
or .bashrc
or .zshrc
depending on which SHELL you use it should set $GOBIN
correctly.
pkg
pkg
Represents the location for package archives, where binaries needed for a certain package import are stored. If a package is required by $GOPATH/src/github.com/username/project
then there is likely a chance, that package will be stored as an archive file inside $GOPATH/pkg
The binary for a package with the import path "X/Y/Z"
lives in $GOPATH/pkg/$GOOS_$GOARCH/X/Y/Z.a
Notes:
Usually artefacts inside bin
and pkg
are generated and no developer interaction is required. Basically all developers care about is $GOPATH/src
where the source code lives.
For more info check out:
Last updated
Was this helpful?