There are three main parts to the Go installation:
-
The
go
binary:This is the main executable file that you will use to compile programs.
-
The standard library:
Go (and all programming languages) ships with a bunch of boiler plate code called the standard library. This library contains code packages that perform useful basic (or sometimes very complex) functions that avoid the need for you to write the code. These code files must be installed in a specific directory on your computer so that the
go
binary knows where to look for them when compiling your program. -
The
GOPATH
You need to create a folder somewhere on your machine that go will primarily use to save dependency files. This folder can have any name but you need to create an enviornmental variable in your operating system called the
$GOPATH
and set it equal to the path of this folder. In prior versions of Go the GOPATH played a larger role in development, typically hosting working code. But with the introduction of a new dependency management system calledModules
the role of theGOPATH
is mostly relegated to storing downloaded and compiled program dependencies.
All the download links in this document will point to the latest version of Go which is version 1.16.6. At some point new versions of Go will be released and while these instructions will remain the same, you will need to change the version in the download link to whatever the latest version is.
These are some things that you should have installed on your machine prior to installing Go. Run the following commands in the terminal.
sudo apt-get update
sudo apt-get install build-essential git -y
You will download a zip file containing the go
binary and library files. This can be downloaded from the browser by clicking here: https://golang.org/dl/go1.16.6.linux-amd64.tar.gz.
Alternatively you can download it via the command line.
cd Downloads
wget https://golang.org/dl/go1.16.6.linux-amd64.tar.gz
The following command will extract the files inside the zip file and move them into the appropriate directory in the file system (in this case /usr/local/go
). Note you must run this cammand from inside the same directory where you downloaded the zipped file (for example Downloads
).
sudo tar -zxvf go1.16.6.linux-amd64.tar.gz -C /usr/local/
Crate a directory to serve as your GOPATH. In the command below we just call it go
.
mkdir $HOME/go
Now let's set our environmental variables. If you run the following command in the terminal it will open a text editor containing a file called your profile.
gedit ~/.profile
At the very end of the file add the following lines and save the file:
GOPATH=$HOME/go
PATH=$PATH:/usr/local/go/bin
PATH=$PATH:$GOPATH/bin
Those lines set the GOPATH
as an environmental variable as well as add the location of the go
binary to your PATH
and add $HOME/go/bin
to your PATH
as well so that you can your compiled programs by just typing their names in the terminal.
Finally run the following command then restart the terminal (close it and open it again so that the profile changes take effect):
source ~/.profile
You will know if go is installed right as you can type:
go version
In the terminal and get the following response:
go version go1.16.6 linux/amd64
These are some things that you should have installed on your machine prior to installing Go. Run the following commands in the terminal.
sudo apt-get update
sudo apt-get install git -y
If homebrew is not installed run the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Install via homebrew
brew install go
Crate a directory to serve as your GOPATH. In the command below we just call it go
.
mkdir $HOME/go
Now let's set our environmental variables. If you run the following command in the terminal it will open a text editor containing a file called your profile.
open $HOME/.bash_profile
At the very end of the file add the following lines and save the file:
export GOPATH=$HOME/go
export GOROOT="$(brew --prefix golang)/libexec"
export PATH="$PATH:${GOPATH}/bin:${GOROOT}/bin"
Those lines set the GOPATH
as an environmental variable as well as add the location of the go
binary to your PATH
and add $HOME/go/bin
to your PATH
as well so that you can your compiled programs by just typing their names in the terminal.
Finally run the following command then restart the terminal (close it and open it again so that the profile changes take effect):
source ~/.profile
You will know if go is installed right as you can type:
go version
In the terminal and get the following response:
go version go1.16.6 darwin/amd64
You need to have Go, Git, and GCC installed.
- Go v1.16
- Git
- TDM-GCC/MinGW-w64
- http://tdm-gcc.tdragon.net/
- A standard installation should automatically add the correct
PATH
, but if it doesn't:- Open the command prompt and type:
setx PATH "%PATH;C:\TDM-GCC-64\bin"
- Open the command prompt and type:
Create a directory to store all your Go projects (e.g. C:\goprojects
):
- Set your GOPATH
- Open the command prompt and type:
setx GOPATH "C:\goprojects"
- Open the command prompt and type:
Type go version
in the command prompt. You should see:
go version go1.16.6 windows/amd64