How to Install Go (Golang)

To install Go (Golang) on Windows, follow these steps:

  1. Download Go: Go to the official Go website at https://golang.org/dl/ and download the latest stable version of Go for Windows. Choose the installer that matches your operating system (32-bit or 64-bit).
  2. Run the Installer: Once the download is complete, run the downloaded installer. The installer will guide you through the installation process.
  3. Choose Installation Location: During the installation, you’ll be asked to choose an installation location for Go. The default location is usually fine, but you can choose a different location if you prefer.
  4. Set Environment Variables: After the installation is complete, you need to set the Go-specific environment variables to use Go from the command prompt.
    • Open the Start menu and search for “Environment Variables.”
    • Click on “Edit the system environment variables” to open the System Properties window.
    • Click the “Environment Variables” button at the bottom of the window.
    • Under “System variables,” click “New” to add a new environment variable.
    • For the “Variable name,” enter GOROOT.
    • For the “Variable value,” enter the path where Go is installed. For example, C:\Go (if you installed it in the default location).
      Next, add the Go binary path to the PATH environment variable:
    • Find the “Path” variable under “System variables” and click “Edit.”
    • Click “New” and add the path to the Go bin directory. By default, it is C:\Go\bin. If you installed Go in a different location, use that path.
  5. Verify Installation: Open a new command prompt window and type go version. If everything is set up correctly, it should display the installed Go version.

To install Go (Golang) on a Linux system, you can use the following steps:

Download Go: Go to the official Go website at https://golang.org/dl/ and download the latest stable version of Go for Linux. Choose the archive file that matches your system’s architecture (usually 64-bit). For example, you might download go1.x.x.linux-amd64.tar.gz.

Extract the Archive: Open a terminal and navigate to the directory where the downloaded archive is located. Use the following command to extract the archive:

tar -C /usr/local -xzf go1.x.x.linux-amd64.tar.gz

This command will extract the Go binaries and tools into the /usr/local directory.

Set Environment Variables: To use Go, you need to set the Go-specific environment variables. Open the .bashrc or .bash_profile file in your home directory using a text editor. You can use either of the following commands:

For Bash:

nano ~/.bashrc

For Zsh:

nano ~/.zshrc

Add the following lines to the file:

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

Save and close the file.

Apply Changes: To apply the changes made to the environment variables, run the following command in the terminal:

source ~/.bashrc

or

source ~/.zshrc

This will refresh the environment variables in the current terminal session.

Verify Installation: Open a new terminal window or run go version in the existing terminal. If everything is set up correctly, it should display the installed Go version.

To install Go (Golang) on a Mac, you can use the following steps:

Download Go: Go to the official Go website at https://golang.org/dl/ and download the latest stable version of Go for macOS. Choose the macOS installer package with the .pkg extension that matches your system’s architecture (usually 64-bit).

Run the Installer: Double-click the downloaded .pkg file to start the installation process. Follow the on-screen instructions to complete the installation. You might need administrator privileges to install Go.

Verify Installation: After the installation is complete, open a terminal window and type the following command to check if Go is installed:

go version

If everything is set up correctly, it should display the installed Go version.

Set Environment Variables: By default, Go is installed in the /usr/local/go directory. However, you need to set the Go-specific environment variables to use Go from the command line. Open the terminal and run the following commands:

echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bash_profile
echo 'export GOPATH=$HOME/go' >> ~/.bash_profile
source ~/.bash_profile

The above commands add the Go binary path to the PATH environment variable and set the Go workspace (GOPATH) to the ~/go directory in your home folder.

Optional: If you are using macOS Catalina (10.15) or later, the default shell is zsh. In that case, update your ~/.zshrc file instead of ~/.bash_profile:

echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.zshrc
echo 'export GOPATH=$HOME/go' >> ~/.zshrc
source ~/.zshrc

If you prefer using the bash shell, you can switch back to it by running chsh -s /bin/bash.