How to Install Go?
Hi there Go enthusiasts! While coding is fun, getting started with the initial setup and development environment might seem like rocket science to many of us. But don’t panic, it only seems difficult until it’s done.
Steps to Install Go
We have compiled a set of steps to set up your Go environment. Let’s get started!
Step 1: Downloading Go’s binary for your OS
- We start with downloading the binary distribution for Go from the official site –.
- Download the latest version of Go binary release based on the operating system you are using and processor features.
- I downloaded the below-archived binary for my 64-bit windows.
- You can also install Go from source following –
Step 2: Install Go on your system
- Once the download is completed, extract the archived folder to a directory on your system.
Windows:
- For Windows, the default directory to Install Go is C:\<GO_package>. Remember to open a new terminal to be able to view the new changes.
- Next set the two environment variables. You can follow this link if you need help with setting up your environment variables –
GOROOT variable: which helps locate where GO has been installed
PATH variable – Add the below value to your Path variable, which should be same as your %GOROOT%\bin
Linux or Mac:
- The default installation directory for Ubuntu or Linux and Mac users is /usr/local/go. The GOROOT environment variable should hence be set to /usr/local/go/bin.
- In Ubuntu, if you want your directory to be a different one and not the default one, you can edit your ~./bash_profile. Add the following entry in the bash_profile file : export GOROOT = /usr/local/<newpath>. You can check your go installation using – `go version`.
Step 3: Creating Go workspace
Run the command: `go env`. You see that your GOPATH is blank. So what is GOPATH? Before this, let us understand what a Go workspace is.
- Go programmers use Go workspaces to organize Go projects. A workspace follows a directory like structure, where Go looks for source code, builds files and resolves package dependencies.
Windows:
- When Go encounters an import statement, it first tries finding for that package in GOROOT/src. If it does not find it here, it looks for the same in GOPATH/src.
- Let’s set our GOPATH now. First, create a directory which signifies your Go workspace. The workspace is usually created at C:\Users\%Username%\goworkspace.
- Now, create GOPATH variable in your environment variables, same as we did before. Add the path of your workspace as the value.
- Add the bin path for the workspace to your path variable.
Linux and Mac OS
- For Linux and Mac systems, we need to follow the below steps. First, open a terminal. Next, edit your ~./bash_profile file. Remember, you may need to add the sudo keyword in Ubuntu systems, so you can give administrator rights. Once you open the file, add the following variables in the file :
- First, set the GOPATH environment variable, pointing to your Go workspace: export GOPATH = $HOME/go
- Next, we need to set our GOPATH/bin variable in ~./bash_profile .This variable helps run the compiled Go programs: export PATH = $PATH:$GOPATH/bin
Step 4: Creating the folder structure required for your Go project
- Our next step is to test if we have set up our Go compiler and variables perfectly. We will be using the Go tool for managing our source code and building our Go project. You can learn more about the Go tool from here -
- The Go tool requires you to organize your source code in a specific structure.
- Let us create this structure now. Navigate to your go workspace. Create three folders and name them bin, pkg, and src respectively.
- The src package contains all the source code and packages. The pkg folder is simply a packaged version of the source code with a .a extension. The bin folder is where all the executable commands lie.
Step 5: Executing your first Go program and testing your installation
- It’s time to write our first Go program now.
- Create a new directory inside the src folder we have just created inside our Go project workspace and name it greetings.
- Next, create a file greeting.go within the greetings directory.
- Type in the following code inside the greetings.go file using any of your favorite editors like Sublime text or Atom. You can download Sublime from here –.
package main
import "fmt"
func main() {
fmt.Printf("Welcome to the world of Gophers!")
}
- Now, we need to build our Go project. We use the `go build` command to help us with this.
- Open your command prompt at greetings directory and run the go build command. This command should build an executable named greetings.exe for you. You can see this in your greeting folder.
- We now just need to execute this executable file. We do this by typing in the executable name, greeting in the command prompt.
- You should see the message “Welcome to the world of Gophers!” on your screen. If you see it, means your installation works fine! Simple isn’t it?
Uninstalling Go
- If you already have a version of Go installed on your system, it is important you uninstall it before installing a newer version.
- We can do this by deleting the Go directory from your system. Like we already, so the default directory where you install Go is C:\<Go_version> for windows. For Linux and Mac systems you need to delete the /user/local/go directory to achieve this.
- Don’t forget to remove Go’s bin directory set in your PATH environment variable too. For Windows, you can check –-
us/windows/desktop/procthread/environment-variables. For Linux users, you should
edit $HOME/.profile file or /etc/profile. Similarly, for MacOs users, the /etc/paths.d/go the file should be deleted.
Well done guys! We are all set with our Go environment. So, what’s next? You can experiment more with Go by taking this cool tour –. You can use any editor of your choice, to type the Go code.
Also, if you have any further queries, you can check out any of the Go forums like these –
Recommended Articles
This has been a guide on how to Install Go. Here we have discussed the basic concept, steps to install Go and also explained the process of uninstalling Go. You may also look at the following articles to learn more –