Install Go

I covered GoLang installation in my previous article, Go Crash Course (Part 1). But I think the article is not deep enough to instruct Go installation. Many of my friends still asked me how to install it, and most had issues with reading private repositories.

I hope this article can help you to install GoLang. Not just one version, but many versions of GoLang. These steps are a bit unusual from the general ones, but they work correctly.

Installation

  1. Go to https://go.dev/dl/.
  2. From the stable versions, download the archive for your OS. Let's say go1.19.5.linux-amd64.tar.gz for Linux.
  3. Make an sdk folder in your home directory, ~/sdk. /home/your-username/sdk for the full path.
  4. Extract the downloaded Go archive file to its version. Here is the full path, /home/your-username/sdk/go1.19.5/<extracted Go files>.
  5. Create a symbolic link to /usr/local/go. For Linux or macOS, you can customize this command to your needs, sudo ln -s /home/your-username/sdk/go1.19.5 /usr/local/go. For Windows, you can append ~/sdk/go1.19.5 to the PATH in System Variables.
  6. Open your terminal and verify it, go version.

Change GoLang Version

You can install it by the CLI instead of downloading the archived file in the web browser because your OS already has GoLang. Let's say you want to use the legacy version of GoLang, Go 1.18.10.

  1. go install golang.org/dl/go1.18.10@latest
  2. go1.18.10 download
  3. Remove the current symbolic link, sudo rm -f /usr/local/go. Create the new one, sudo ln -s /home/your-username/sdk/go1.18.10 /usr/local/go. For Windows, you can change the GOROOT and PATH in System Variables.

System Variables

It is better to put GOPATH and GOROOT into System Variables and append them into the PATH.

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

Linux

  1. You can create a file of .bash_profile in your home directory if the file does not exist
  2. Copy and paste the commands above to the file. Save and close.
  3. Put source ~/.bash_profile at the end of the ~/.bashrc file.

macOS

  1. You can create a file of .zprofile in your home directory if the file does not exist
  2. Copy and paste the commands above to the file. Save and close.

Windows

You can put them on System Variables.

Read Private Repositories

Here are frequent issues when reading private repositories in Go.

I can't run go mod tidy because there are private repositories on the go.mod file

Make sure that you are a member or collaborator of those repositories.

The text editor always asks for the credential every time runs go mod tidy

Create a .netrc (or _netrc for Windows) file for the auto-login process on your home directory. For more information about the .netrc file, you can read this article https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html. Here is an example of a .netrc file for GitHub.

machine github.com
  login YOUR_USERNAME
  password YOUR_PASSWORD_OR_PERSONAL_ACCESS_TOKEN

Already a member and have a .netrc file, but still can't read private repositories when running go mod tidy

You need to add the domain to the GOPRIVATE. Let's say you want to read private repositories from GitHub and GitLab, then go env -w GOPRIVATE="github.com,gitlab.com". Go check it with go env GOPRIVATE.

Related Articles