How to install Go on your computer.
How to compute a checksum (SHA 256).
Computer architecture
Hash Function
Compression / Decompression
Shell Profile
Path
To write Go programs, you need an editor. To test, document, compile, format your Go programs, you will need to install the Go toolchain binaries.
\nThis chapter will guide you through the installation process.
\n\nIn the following sections, you will need to know your computer architecture. This technical term sometimes brings confusion to newcomers. What is it exactly?
\nComputers come in different shapes, prices and forms. A lot of consumers focus on the brand on the computer and their high level specifications (like RAM, amount of storage on the hard drive). The question of the processor is sometimes eluded by marketing, but it’s a fundamental part of the computer. The processor is also called the CPU (Central Processing Unit).
\nThe CPU is responsible for running system instructions but also programs. CPU is a generic term. There are different types of CPUs available.
\nPrograms use a set of predefined instructions to speak to the machine. We call this list of orders the instruction set. Some processors share the same instruction sets. Others have entirely different ones. The architecture notion covers the instruction set and the physical organization, and computer implementation.
\nYou understand now why this notion is essential at the machine level. The Go toolchain is a collection of programs that we will use to build our programs.
\nThose programs are written in Go and compiled.
The Go team provide different versions of the toolchain for each OS and architecture supported
\nGo has a minimum system and OS requirements.
You can check them on this webpage:
\nhttps://github.com/golang/go/wiki/MinimumRequirements.
The page is updated frequently.
To start developing a Go program, you have first to install Go on your computer. To install it, you will have to download a file. The installation procedure is slightly different depending on your OS (Operating System). Just follow the steps corresponding to your OS.
\nThe first step (which does not depend on your OS) is to go to the official download page : https://golang.org/dl/. By the way, golang.org is the official Go website. We strongly advise you to download Go only from this URL.
\nIn this section, we will cover Linux, macOS, and Windows. Note that go is also available for FreeBSD.
\n\nYou have to determine the architecture of your computer first. It will allow you to select the right file to download.
\nOpen a terminal and type the following command :
\n$ uname -p
\nUname is a program that displays characteristics of your system. The flag -p will display “print the machine processor architecture name”.
\nGolang Linux versions are available for the following architectures :
\nx86
x86-64
ARMv6
ARMv8
ppc64le
s390x
When you know your architecture, you can now download the corresponding compressed folder on the Go website :
\nJust click on the blue link, and the download will begin.
\nIn the following sections, we will install version 1.12.8 for the architecture x86-64. The instructions are the same for other architectures and other versions. Only the name of the archive will change.
\n\nThis step is strongly advised. The objective is to check that you do not have a corrupted version of the archive. To do that, open a terminal and type :
\n$ cd /where/you/have/downloaded/the/file\n$ sha256sum go1.12.8.linux-amd64.tar.gz
\nThe first command use cd (change directory) to go to the directory where the file you have downloaded has been put. For instance, if the file is in /home/max/ you have to execute :
\n$ cd /home/max
\nThe next command will compute the SHA256 hash (which is a cryptographic hash function) of the file go1.12.8.linux-amd64.tar.gz.
\nCryptographic hash function: A function that will take as input variable size data (a file, a word, a sentence) and output a fixed size data blob. Having the output, it’s almost impossible to retrieve the function’s input. The result is called the “hash” or the “message-digest”. Here we use it to ensure that the file has not been altered in its way to our computer. To do that, we compare the hash that we computed on our computer with the hash provided on the Go website. If both are equal, there has been no alteration.
\nWhen the sha256sum has been executed, you will see on your screen a set of characters that you have to compare with the hash displayed on the website. If there are not identical, something went wrong, do not use the file downloaded. If the hash you have computed and the hash displayed on the Go website is equal, your good to go.
\n\nThe archive is gzipped (compressed with gzip, it’s often called a tarball). To deflate it, you have two options :
\nUse the graphical interface (if you have one): double click on the archive and a window will open. Follow the instructions. You have to extract and deflate the files in /usr/local
Use the terminal.
We will take option 2.
\nOpen your terminal and type the command :
\n$ sudo tar -C /usr/local -xzf go1.12.8.linux-amd64.tar.gz
\nWe will use the application tar (in sudo mode)
\n-C /usr/local : means that we will change the exection directory to /usr/local
-xzf go1.12.8.linux-amd64.tar.gz: means that we want to extract (x) the archive go1.12.8.linux-amd64.tar.gz that is compressed with gzip.
Let’s take a look at the tree view of the folder /usr/local/go :
\nWe have eight folders: API, bin, doc, lib, misc, pkg, src, and tests.
\nThe folder bin contains two executables :
\ngo : this is the main executable
godoc : this is a program used to generate documentation1
gofmt : this program will format your source files according to the language conventions
If you are into the bin directory, you can launch go. Try this command :
\n$ ./go version
\nIt will print the go version. But this is not satisfying. We want to be able to open a terminal and run go –version everywhere. To do that, we need to add this folder to the PATH.
\nPATH: an environment variable named PATH that contains a list of directories where shells will search for executable files when a user issues a command. You will often hear “add this directory to the path”. This expression means that you have to append a directory address to the list of directories in the PATH environment variable.
\nThe following command will append “/usr/local/go/bin” to the PATH variable :
\n$ export PATH=$PATH:/usr/local/go/bin
\nWe set the variable PATH (with the export command) with the value already contained in it (denoted by $PATH) and we append to it the characters “:/usr/local/go/bin”. If you keep the same terminal open and type :
\n$ go version
\nyou will see the version of Go displayed.
\nNow, if you close your terminal and open another one, you will notice that it does not work anymore. The environment variable PATH has been modified only for your current session. When you open a new terminal, you create a new session. The modification made to PATH is propagated to new sessions.
\nTo modify the PATH variable for each new session, you have to change your shell profile. You need to add “export PATH=$PATH:/usr/local/go/bin” at the beginning of the profile.
\nIf you use bash the file to modify is ~/.bashrc. It is run whenever a session is started
If you use zsh the file to modify is ~/.zshrc. It is run whenever a session is started
For other shells, refer to the documentation provided, but it should work the same
When .bashrc or .zshrc has been modified, the change is not applied immediately. You need to start a new session to see the result.
\nIf you do not want to start a new session, you can type this command and press enter :
\n$ source ~/.bashrc
\nOpen a terminal and ask Go for its current installation version :
\n$ go version
\nThe version number should appear!
\n\nYou have two main options to install go :
\nUse the installer
Download the binary and install it yourself.
A third option would be to compile go from sources. For simplicity, we will show you how to use the installer.
\n\nGo to the website https://golang.org/dl/ and download the appropriate installer for the version you desire. On the figure 1 we have highlighted the line corresponding to the installer version 1.12.8 for macOS.
\nTo verify the integrity of the file that downloaded, we will compute a SHA256 cryptographic hash. To do that, open a terminal window. Then type the commands :
\n$ cd Downloads\n$ shasum -a 256 go1.12.8.darwin-amd64.pkg SHA_SUM go1.12.8.darwin-amd64.pkg
\nThe first command will change the current directory (cd) to /Downloads. Then we will use the tool shasum. We add the flag a to say that we want to use the SHA256 hashing function. The result will be composed of the cryptographic hash followed by the name of the file hashed.
\nTo verify the hash, go to the download page and check the SHA256 checksum column. You need to verify that the hash you get is the same as the one given by the go team.
\nReplace SHA_SUM by the value you copied on the website.
\nOn figure 2 you can see that we have equality between hashes.
\nHashes that do not correspond means that the file that you downloaded has been somehow modified. You should not use it.
\n\nDouble click on the downloaded file. It will launch the installation wizard automatically. Follow the installation process. You will be asked for your password during the installation. The installer will put the files needed to run Go into the directory /usr/local/go.
\nOpen a terminal and type :
\n$ go version
\nIf the version of go is displayed, then your good to go! Otherwise, you will need to add Go to your PATH environment variable.
\nYou can follow the instruction in the previous section. They are the same for macOS.
\n\nIn this section, we will show you how to install go with the .msi installer. This is the easiest method. But first, take a look at the specific requirements for Windows operating systems: https://github.com/golang/go/wiki/MinimumRequirements#windows.
\n\nGo for Windows is available for two architectures :
\nx86, which correspond to 32 bits systems
x86-64, which correspond to 64 bits systems
To know if your system is either 32 bits or 64 bits, you will have to check the system properties :
\nFor Windows 7
\nClick on the Start button
Right-click on Computer
Then click on Properties
A window will show the System type
For Windows 10 and Windows 8.1
\nClick on the Start button
Then go to Settings, then System and About
A window will show the System type
Now that you know your architecture, you can download the appropriate installer on the official Go website :
\nOnce you have downloaded the correct installer, you have to check its integrity. To do that, we will compute the SHA256 hash of the downloaded file.
\nWindows has a builtin program called certutil.exe.This program is part of the “Certificate Service”. This program is located in C:\\Windows\\System32\\certutil.exe. If you do not find it, I suggest you to launch a search into the Windows folder.
\nTo compute the SHA256 hash, open a terminal and then type the following commands.
\n$ cd C:\\users\\max\\downloads\n$ certutil -hashfile go1.12.8.windows-amd64.msi SHA256\nSHA256 hash of file go1.12.8.windows-amd64.msi:\n98 39 25 49 90 de dc 56 47 bf 38 61 1f 7c 1a 7a fc 65 f7 fd 6a af f9 77 e0 5b 17 d4 ec 21 fe a6\nCertUtil: -hash file command completed successfully.
\nYou have then to compare the hash computed by certutil to the one displayed on the golang.org website. The string output by certutil has spaces between each hexadecimal number. You have to copy the string and remove the spaces.
\nIf both strings are equal, you can go to the next step. If not, you get a corrupted version. Do not use it.
\n\nDouble click on the installer and follow the instructions. At the end of the installation process, test your installation. Open a terminal and type the following command :
\n$ go version
\nIf you see the version printed, you have finished your installation
\n\nGo use environment variables for its configuration. In this section, we will detail some of them :
\nGOBIN
: By default, Go will place compiled programs into $GOPATH/bin. If you want to override this behavior, you can set this variable.
GOROOT
: The absolute path where the Go distribution is installed (for Linux and macOS user, it’s by default /usr/local/go).
GOHOSTOS
: This is the operating system of the Go toolchain
GOHOSTARCH
: This is the system architecture of the Go toolchain binaries
To print all the Go environment variables, you can use this command :
\n$ go env
\nUse this command when you have trouble with your Go settings. Here is the output of the command :
\nGO111MODULE=""\nGOARCH="amd64"\nGOBIN=""\nGOCACHE="/Users/maximilienandile/Library/Caches/go-build"\nGOENV="/Users/maximilienandile/Library/Application Support/go/env"\nGOEXE=""\nGOFLAGS=""\nGOHOSTARCH="amd64"\nGOHOSTOS="darwin"\nGOINSECURE=""\nGOMODCACHE="/Users/maximilienandile/go/pkg/mod"\nGONOPROXY=""\nGONOSUMDB=""\nGOOS="darwin"\nGOPATH="/Users/maximilienandile/go"\nGOPRIVATE=""\nGOPROXY="https://proxy.golang.org,direct"\nGOROOT="/usr/local/go"\nGOSUMDB="sum.golang.org"\nGOTMPDIR=""\nGOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"\nGOVCS=""\nGOVERSION="go1.16"\nGCCGO="gccgo"\nAR="ar"\nCC="clang"\nCXX="clang++"\nCGO_ENABLED="1"\nGOMOD="/dev/null"\nCGO_CFLAGS="-g -O2"\nCGO_CPPFLAGS=""\nCGO_CXXFLAGS="-g -O2"\nCGO_FFLAGS="-g -O2"\nCGO_LDFLAGS="-g -O2"\nPKG_CONFIG="pkg-config"\nGOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/lm/9l2tk4811x32rmw4407f9h3m0000gn/T/go-build744222834=/tmp/go-build -gno-record-gcc-switches -fno-common"
\n\nIn this section, you can find the list of common issues encountered during Go’s installation.
\n\nWhen using zsh shell (on Linux or Mac) :
\n$ go version\nzsh: command not found: go
\nWhen using bash shell (on Linux or Mac) :
\n$ go version\nbash: command not found: go
\nFor Windows:
\n$ go version\n'go' is not recognized as an internal or external command, operable program Or batch file.
\n\nThis error message means that the “go” executable is not found by your shell. When you open a terminal, the shell will load the environment variable PATH (see the definition in the previous section). This variable contains a list of directory addresses on the filesystem. The shell will look in those directories if an executable file named “go” It can be found. If no executable named “go” can be found, it will output an error.
\n\nVerify that the folder that contains the “go” binary (go/bin) is added to your PATH.
\nFor Linux and Mac users :
\nOpen a terminal and type “echo $PATH”
Check that the directory go/bin is in the output result
\n$ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
Here you can see that /usr/local/go/bin is in the path
If it’s not add /usr/local/go/bin to the path (follow the instruction provided in the previous section)
For Windows users :
\nC:> echo %PATH% C:;C:;C:
Here the setup is correct. The bin folder is in the path
If it’s not, add it to the PATH. To do so :
\nopen System > Advanced system settings
then click on the button Environment Variables. A window containing all environment variables should appear.
Click on “PATH”
Add a semicolon and then “C:\\Go\\bin”
Click OK then OK.
Close your terminal window and open another one. You should be good.
If the previous solution did not work, you should check that the “go” binary is effectively present where you think it is.
\nFor Linux and Mac users :
\nFor Windows users :
\nWhen for instance, the folder Go (or go) do not even exist, you will need to reinstall it. Maybe you put the installed binary into another directory?
The go binary is found in your system, but when you launch a Go command you get an error :
\n$ go version\nzsh: exec format error: go
\n\nYou probably downloaded a version that does not conform to your system. For instance, you download the binary for Linux and launch it on your mac.
\n\nDownload the version that corresponds to your operating system and your computer architecture.
\n\nHow to output the version number of Go?
Where to find the latest version of Go?
How to output the version number of Go?
\nOpen a terminal
Type
\n$ go version
Press enter
Where to find the latest version of Go?
\nThe Go toolchain is available on the Go official website
The Go toolchain is available on Windows, macOS, and Linux Operating systems
You should download the version that matches your OS and your Architecture.
After installing the Go toolchain, you can launch the go command with your favorite terminal.
\nIn most recent versions godoc has been removed from the bin directory.↩︎
Previous
\n\t\t\t\t\t\t\t\t\tThe terminal
\n\t\t\t\t\t\t\t\tNext
\n\t\t\t\t\t\t\t\t\tFirst Go Application
\n\t\t\t\t\t\t\t\t