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:

├── $GOPATH/
│   ├── src/
│   │   ├── github.com/
│   │   │   ├── organization/
│   │   │   │   ├── project1/
│   │   │   │   └── project2/
│   │   │   ├── user1/
│   │   │   │   ├── project1/
│   │   │   │   └── project2/
│   │   │   ├── pgk/
│   │   │   └── └── errors/
│   │   ├── gitlab.com/
│   │   │   ├── organization/
│   │   │   │   ├── project1/
│   │   │   │   └── project2/
│   │   │   ├── user1/
│   │   │   │   ├── project1/
│   │   │   │   └── project2/
│   │   │   ├── user2/
│   │   │   │   ├── project1/
│   │   │   └── └── project2/
│   │   ├── golang.org/
│   │   │   ├── x/
│   │   │   │   ├── crypto/
│   │   │   │   ├── lint/
│   │   │   └── └── sys/
│   ├── bin/
│   │   ├── goimports
│   │   ├── golint
│   │   ├── binary1
│   │   └── binary2
│   ├── pkg/
│   │   ├── darwin_amd64/
│   │   │   ├── github.com/
│   │   │   │   ├── uber-go/
│   │   │   │   │   └── atomic.a
│   │   │   │   ├── user1/
│   │   │   │   │   ├── package1/
│   │   │   └── └── └── package2/
│   │   ├── mod/
│   │   │   ├── github.com/
│   │   │   │   ├── uber-go/
│   │   │   │   │   └── zap@1.8.0
│   │   │   │   │   └── zap@1.9.1
│   │   │   └── └── └── ...
│   │   ├── dep/
│   │   │   ├── sources/
│   │   │   │   ├── https---github.com-pkg-errors/
│   │   │   │   │   ├── errors.go
│   │   │   │   │   ├── errors_test.go
│   │   │   │   │   └── ...
│   │   │   │   ├── https---github.com-user1-project1/
│   │   │   │   │   ├── file1.go
│   │   │   │   │   ├── file1_test.go
│   │   ├── ├── ├── └── ...
│   │   │   │   ├── https---github.com-user2-project1/
│   │   │   │   │   ├── file1.go
│   │   │   │   │   ├── file1_test.go
│   │   │   │   │   ├── file2.go
│   │   │   │   │   ├── file2_test.go
│   │   └── └── └── └── ...
│   └──
└──

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

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.

export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

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