Go Installation and Environment Setup

In this article, we will learn all the steps that are necessary to set up Go in your computer system. Go(or Golang) is available for different major operating systems whether it is Mac OS, Windows, or Linux.

It should be noted that we can run Go code on any browser without the need for a local setup by just visiting the Go Playground. To visit the Go playground follow this link. The Go Playground is a great tool that comes in very handy when you want to check few things on the go, but it is very much recommended that you must have Go installed on your local machine as there are many things that you will not be able to do on Go playground.

Limitations of Go Playground

  • Code Restrictions

  • CPU Limitations

  • Can't access some standard library packages like io/ioutil or os.

Most of the above limitations won't be a hindrance if you just want to run a sample code that doesn't do any I/O operations or anything machine-specific, but still, in long run, you can't rely on Go playground for all the code that you will write might not run on Go Playground.

Installing Go

We will talk about all the three major operating system platforms whether it is macOS, Windows, or Linux. Installing Go on all these operating systems varies a bit.

The first thing that you need when installing Go on your local machine is the stable binary release for your operating system. The team that manages Go provides stable binary releases for all the major operating systems. You can download the stable binary release for your operating system by visiting this link.

Downloading GO

Installing Go on Windows

For Windows operating system, we need to download the stable MSI release that is present on the binary release download link. After downloading the MSI, you need to open it and follow all the prompts that the installer asks you to. After installation, now, open the command prompt on your machine. You can open the command prompt by following these steps:

  • Click the Start menu.

  • In the menu's search box, type cmd, then press the Enter key.

Once the command prompt is open, you need to verify that Go is installed without any issues, and to do that you need to write the following command in your command prompt :

go version

The above command is used to check which Go version is installed on your operating system. The output of the above command should look something like this:


go version go1.15.5 windows/amd64

If your output is something like this, then you are good to go. Go is installed on your operating system and all you need is to set up the environment before you start working with Go. We will learn this later in this article.

Installing Go on Linux

Note: It is recommended that you install Go on root user. You can switch to root, by typing sudo su in your terminal.

For Linux-based operating systems, we need to download the tar file that is present on the download page of the site. After downloading the tar file, we need to open the terminal and write the following command to set up Go on our Linux machine.

rm -rf /usr/local/go && tar -C /usr/local -xzf goX.XX.X.linux-amd64.tar.gz

It should be noted that the X is a placeholder and it must be replaced with whatever version's tar you have extracted, for example, if you have downloaded the tar for go version go1.15.5 then replace the X accordingly.

Go Setup Linux

The above command is a combination of two commands, the first command( before the && ) is used to remove any old version of Go that might be installed on your machine and the second command( after the && ) is used to extract the Go source code inside the /usr/local directory.

After this command we need do to one more thing, that is to add the /usr/local/go/bin to the PATH variable that we have inside the bashrc file. Follow the commands shown below:

vi ~/.bashrc

After opening the bashrc by the above command, we need to add the below command at the end of the bashrc file.

export PATH=$PATH:/usr/local/go/bin

Go Setup Linux

Now, one last command is to source the bashrc and we are done. Just type the below command:

source ~/.bashrc

Now just confirm that Go is installed by running the following command:

go version

The output should look something like this:


go version go1.15.5 linux/amd64

Installing Go on macOS

Installing Go on macOS is probably the simplest. Just download the pkg from the download link of official site and all you need to do is just follow the prompts that come on the installer. After doing this, a prompt will open saying that Go is installed. The package basically installs the Go distribution inside the /usr/local/go folder on your mac.

You can always verify that whether Go is installed properly or not, the way to do so is just to write the below command on the terminal:

go version

The output should look something like this:


go version go1.15.5 darwin/amd64

Setting the Go Environment in Windows

Before being able to run Go programs and build Go projects, we need to have the right environment set up on our local machines. It should be noted that in the case of Windows we need to open Advanced System Settings, and click the Environment Variables button and then make an entry for GOPATH and GOROOT in that.

The first thing we will do is set the two most important Go environment variables, GOPATH and GOROOT. Both these environment variables are very important.

GOPATH - It is the path to the directory where all the code you will write will be present.

GOROOT - It is the path to the directory where the Go source code is present.

Setting the Go Environment in Linux/macOS

It should be noted that GOPATH can be set to any directory in your system, just make sure that Go is installed on that user if you are using Linux or macOS. To set GOPATH, open the bashrc file and type the following commands in it and then save the file.

export GOPATH=/root/go_projects

In the above command, I choose to keep my Go code inside the /root/go_projects directory. It's totally up to you to choose any directory you want. Now inside the go_projects folder, we need to have the three subfolders, mainly:

  • pkg - the subfolder that will contain the packages and shared object files if any.

  • src - the subfolder where all the code you will write will be stored.

  • bin - the subfolder which will contain all the binary executables that you will make.

It should be noted that mostly we need to work only inside the src directory.

Setting the GOROOT is also important. To do that we need to open the bashrc file again and type in the below command and then source it.

export GOROOT=/usr/local/go

Now just source the bashrc file to save the changes and everything is set.

Go Setup

Conclusion

In the above article, we learned the limitations of the Go playground, followed by how to install Go on different operating systems, and lastly, we learned how to set the Go environment on your local machine.