Updated April 7, 2023
How to Install Go?
The following article provides an outline for How to Install Go? 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.
Step 1: Downloading Go’s binary for your OS
- We start with downloading the binary distribution for Go from the official site – https://go.dev/dl/.
- Download the latest version of the Go binary release based on the operating system you are using and processor features.
- Downloaded the below-archived binary for my 64-bit windows.
- You can also install Go from the source following – https://go.dev/doc/install/source.
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 –
https://docs.microsoft.com/en-us/windows/desktop/procthread/environmentvariables
GOROOT variable:
- Which helps locate where GO has been installed.
PATH variable:
- Add the below value to your Path variable, which should be the 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 that signifies your Go workspace. The workspace is usually created at C:\Users\%Username%\goworkspace.
- Now, create the GOPATH variable in your environment variables, the 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 to give administrator rights.
Once you open the file, add the following variables in 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 – https://golang.org/cmd/go/.
- 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 – https://www.sublimetext.com/3.
Code:
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 the 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, 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 – https://docs.microsoft.com/en- 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 – https://tour.golang. You can use any editor of your choice to type the Go code.
If you have any further queries, you can check out any of the Go forums like these – https://forum.golangbridge.org.
Recommended Articles
This has been a guide to How to Install Go? Here we have discussed the basic concept, steps to install Go, and explained uninstalling Go. You may also look at the following articles to learn more –