Setting up the Go environment on your computer is the first step to start programming in Go. In this chapter, we will follow simple steps to get Go installed and ready to use.
Step 1: Download Go
- Go to the official Go website.
- Choose the download link for your operating system (Windows, macOS, or Linux).
- Download the installer file.
Step 2: Install Go
On Windows
- Open the downloaded
.msifile. - Follow the installer instructions. It will install Go to
C:\Goby default. - After the installation is complete, the Go binaries will be added to your PATH environment variable.
On macOS
- Open the downloaded
.pkgfile. - Follow the installer instructions. It will install Go to
/usr/local/goby default. - Ensure that the Go binaries are in your PATH. You may need to add
export PATH=$PATH:/usr/local/go/binto your shell profile (e.g.,.bash_profile,.zshrc).
On Linux
- Open a terminal.
- Navigate to the directory where you downloaded the Go tarball.
- Extract the tarball using the following command:
tar -C /usr/local -xzf go1.xx.x.linux-amd64.tar.gzReplace
go1.xx.x.linux-amd64.tar.gzwith the name of the downloaded file. - Add Go to your PATH by adding the following line to your shell profile (e.g.,
.bashrc,.zshrc):export PATH=$PATH:/usr/local/go/bin - Apply the changes by running:
source ~/.bashrcor
source ~/.zshrc
Step 3: Verify the Installation
- Open a terminal or command prompt.
- Type the following command:
go version - You should see output similar to:
go version go1.xx.x <OS/ARCH>This confirms that Go is installed correctly.
Step 4: Set Up the Workspace
- Create a directory for your Go workspace. This is where your Go projects will be stored.
mkdir -p $HOME/go/{bin,src,pkg}This creates the
bin,src, andpkgdirectories inside yourgodirectory. - Set the
GOPATHenvironment variable to point to your workspace. Add the following line to your shell profile:export GOPATH=$HOME/go - Add the
bindirectory to your PATH. Add the following line to your shell profile:export PATH=$PATH:$GOPATH/bin - Apply the changes by running:
source ~/.bashrcor
source ~/.zshrc
Conclusion
You have now successfully set up the Go environment on your computer. You are ready to start developing applications using Go. In the next post, we will create your first Go program.