So you’re ready to take the plunge and install Go on your local system? Congratulations!

The official Download and Install instructions get you quickly up and running with Go.

This guide walks you through those steps, discusses the Go environment, and also explains how to setup your PATH for running any Go programs you install.

Prerequisites

You should already have completed:

Installing Go

Note: this guide is running on Linux. For Mac or Windows steps, see the official Download and Install instructions.

Start in your home directory:

$ pwd
/home/gopher

Download the latest version of Go:

$ wget -q https://golang.org/dl/go1.19.1.linux-$GOARCH.tar.gz

Extract and install:

$ sudo tar -C /usr/local -xzf go1.19.1.linux-$GOARCH.tar.gz

Add the install target to your profile PATH:

$ echo export PATH="/usr/local/go/bin:$PATH" >>$HOME/.profile

Source your profile to test the new settings:

$ source $HOME/.profile

Verify the Go installation:

$ go version
go version go1.19.1 linux/amd64

The Go environment

The go command and the tools it invokes consult environment variables for configuration. If an environment variable is unset, the go command uses a sensible default setting.

Let’s examine the default settings in your setup:

$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/gopher/.cache/go-build"
GOENV="/home/gopher/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/gopher/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/gopher/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.1"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

To see the effective setting of a specific variable, for example GOBIN, you can run:

$ go env GOBIN

To change the default setting of a variable, for example GOBIN, you can run:

$ go env -w GOBIN=/path/to/my/gobin

Check the new value is set:

$ go env GOBIN
/path/to/my/gobin

Defaults changed in this way are recorded in a Go environment configuration file stored in the per-user configuration directory, as reported by os.UserConfigDir. The location of the configuration file can be changed by setting the environment variable GOENV.

Print the effective location of the configuration file:

$ go env GOENV
/home/gopher/.config/go/env

Note, you cannot change the default location of the configuration file.

Unset GOBIN, returning to its default value:

$ go env -w GOBIN=

Check the effective value of GOBIN now:

$ go env GOBIN

That is to say, GOBIN is not set by default.

See go help env for more details.

Setting up your PATH

Note: this section applies to Linux and Mac users only.

go get and go install install programs to $GOPATH/bin, or $GOBIN if set. GOBIN is not set in the play-with-go.dev environment:

$ go env GOBIN

So you need to add $GOPATH/bin to your profile PATH:

$ echo export PATH="$(go env GOPATH)/bin:$PATH" >>$HOME/.profile

Source your profile again to test the new settings:

$ source $HOME/.profile

Verify the new setting of PATH:

$ echo $PATH
/home/gopher/go/bin:/usr/local/go/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

Conclusion

That’s it! You’re all set for working with Go on your local system.

As a next step you might like to consider:

  • Go Fundamentals