Instructions on how to install the Go programming language on macOS.
First step should be to unsinstall any previous Go installation. This step can be skipped if no Go version was previously installed.
To uninstall any previous Go installations use the following commands:
sudo rm -rf /usr/local/go
The Go programming language can be obtained here. Copy the link for the archive version that you want to install from there.
Get the Go programming language archive and extract it with the following command:
sudo mkdir -p /usr/local/go
curl --silent --location --retry 3 "https://golang.org/dl/go1.16.5.darwin-amd64.tar.gz" | sudo tar xz --no-same-owner --strip-components=1 -C /usr/local/go
Go tools expect a certain layout of the source code. To create the required folders use the following commands:
mkdir -p ${HOME}/Documents/Projects/Go/{bin,pkg,src}
To make go
use those folders the following lines will have to be added to
the .bash_profile
file:
export GOROOT="/usr/local/go"
export GOPATH="${HOME}/Documents/Projects/Go"
export GOBIN="${GOPATH}/bin"
export PATH="${PATH}:${GOROOT}/bin:${GOBIN}"
Write your code inside the ${HOME}/Documents/Projects/Go/src
folder.
Open a new terminal window and check if the Go programming language is installed:
go version
go env
Also check the value of the environment variables - on the output of the
go env
command.