James's Ramblings

Go

Created: July 08, 2022

Environment variables

Official documentation

GOROOT

The path to the installed Go SDK. Examples:

/usr/lib/golang
/nix/store/SOME_NIX_PATH/share/go

GO111MODULE

In modern versions of Go, this variable determines whether the new Go modules behaviour is used or the older GOPATH behaviour. The default is on in modern versions of Go and this must be toggled off to use the old behaviour.

With the GOPATH behaviour, each dependency was installed in $GOPATH/src. GOPATH had to be set on a per project basis.

The modern behaviour is a go.mod file in each project tracks dependencies.

GOPATH

If set and GO111MODULE=off the root directory of the current Go workspace.

GOBIN

The directory that go install will install binaries.

Creating a go.mod

In order to create a go.mod file to track dependencies, name the local package by adding package PACKAGENAME to the top of the file.

Determine the repository in which the Go module will be created.

Then:

go mod init example.com/repository