Skip to content

Instantly share code, notes, and snippets.

@cpacia
Last active July 18, 2021 15:55
Show Gist options
  • Save cpacia/c58bed6eeecf9455873aeb2b1c664752 to your computer and use it in GitHub Desktop.
Save cpacia/c58bed6eeecf9455873aeb2b1c664752 to your computer and use it in GitHub Desktop.
Installing Go

Go

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 called Modules the role of the GOPATH is mostly relegated to storing downloaded and compiled program dependencies.

A Note About Versions

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.

Install By Operating System

Linux

Prerequisites

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

Download Go

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

Extract And Install

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/

Setup GOPATH

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

Mac

Prerequisites

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 Go

Install via homebrew

brew install go

Setup GOPATH

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

Windows

Prerequisites

You need to have Go, Git, and GCC installed.

Setup GOPATH

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"

Type go version in the command prompt. You should see:

go version go1.16.6 windows/amd64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment