Step-by-Step Guide to Installing Go (Golang) on Windows, Linux, and Mac
Golang, also known as Go, is a statically typed, compiled programming language designed by Google. It’s known for its simplicity, efficiency, and concurrency support, making it a popular choice for modern software development. This guide will walk you through the installation process for Go on Windows, Linux, and Mac, and explain the important environment variables and their setup.
Installing Go on Windows
Download Go Installer:
- Go to the official Go download page.
- Download the Windows installer (MSI file).
Run the Installer:
- Locate the downloaded MSI file and double-click it to run the installer.
- Follow the prompts in the installer to complete the installation.
Set Up Environment Variables:
- Go sets up the necessary environment variables during installation, but you can verify them.
- Open Command Prompt and type
go version
to check if Go is installed correctly. - If necessary, manually add Go’s bin directory to your
PATH
: - Right-click on “This PC” > “Properties” > “Advanced system settings” > “Environment Variables”.
- Under “System variables”, find the
Path
variable and click "Edit". - Add a new entry for
C:\Go\bin
.
Verify Installation:
- Open Command Prompt.
- Type
go version
and press Enter. You should see the installed version of Go.
Installing Go on Linux
Download Go Tarball:
- Open your terminal.
- Navigate to the official Go download page and copy the link to the Linux tarball.
- Use
wget
orcurl
to download the tarball. For example: wget https://golang.org/dl/go1.x.x.linux-amd64.tar.gz
Extract the Tarball:
- Extract the tarball to
/usr/local
directory:
sudo tar -C /usr/local -xzf go1.x.x.linux-amd64.tar.gz
Set Up Environment Variables:
Open your .profile
or .bashrc
file in your home directory:
nano ~/.profile
Add the following lines at the end of the file:
export PATH=$PATH:/usr/local/go/bin export GOPATH=$HOME/go export GOBIN=$GOPATH/bin
Save the file and reload it:
source ~/.profile
Verify Installation:
View terminal window.
Type go version
and press Enter. You should see the installed version of Go.
Installing Go on Mac
Download Go Package:
- Go to the official Go download page.
- Download the macOS package (PKG file).
Run the Installer:
- Locate the downloaded PKG file and double-click it to run the installer.
- Follow the prompts in the installer to complete the installation.
Set Up Environment Variables:
- Go sets up the necessary environment variables during installation, but you can verify them.
- Open Terminal and type
go version
to check if Go is installed correctly. - If necessary, manually add Go’s bin directory to your
PATH
:
nano ~/.profile
Add the following lines.
export PATH=$PATH:/usr/local/go/bin export GOPATH=$HOME/go export GOBIN=$GOPATH/bin
- Save the file and reload it:
~/.profile
Verify Installation:
- Open Terminal.
- Type
go version
and press Enter. You should see the installed version of Go.
Important Go Environment Variables
GOROOT:
Specifies the directory where Go is installed. This is typically set automatically during installation.
Example: /usr/local/go
.
GOPATH:
Specifies the workspace directory for Go projects. This is where your Go code, packages, and binaries will be stored.
- Example:
$HOME/go
.
GOBIN:
- Specifies the directory where
go install
will place binaries. By default, it is set to$GOPATH/bin
. - Example:
$GOPATH/bin
.
PATH:
- Ensures that the Go binary executables can be accessed from the command line. It should include the Go binary directory (
GOROOT/bin
) and your workspace's bin directory (GOBIN
). - Example:
$PATH:/usr/local/go/bin:$GOPATH/bin
.
Installing Go Versioning Manager
Go versioning manager or gvm is very useful to allow you have multiple version of go in your platform.
Here is the steps to install it
Install Bison:
sudo apt-get install bison
Install gvm:
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Installing Go version
gvm install go1.4
To display list of installed go version use
gvm list
To use specific version :
gvm use go1.4 [--default]
Writing Your First Go Program
Now that you have Go installed, let’s write a simple “Hello, World!” program to verify everything is set up correctly.
Create a New Directory:
- Create a new directory for your Go workspace:
mkdir ~/go-workspace cd ~/go-workspace
Create a New Go File:
- Create a file named
hello.go
:
nano hello.go
Add the following code to hello.go
:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Run Your Go Program:
- Open your terminal, navigate to the directory containing
hello.go
, and run:
go run hello.go
You should see the output:
Hello, World!
Congratulations! You have successfully installed Go and run your first Go program on Windows, Linux, and Mac. By understanding and setting up the essential environment variables, you can efficiently manage your Go workspace and projects.