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
- Go to https://go.dev/dl/.
- From the stable versions, download the archive for your OS. Let's say go1.19.5.linux-amd64.tar.gz for Linux.
- Make an
sdkfolder in your home directory,~/sdk./home/your-username/sdkfor the full path. - Extract the downloaded Go archive file to its version. Here is the full path,
/home/your-username/sdk/go1.19.5/<extracted Go files>. - 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.5to thePATHin System Variables. - 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.
go install golang.org/dl/go1.18.10@latestgo1.18.10 download- 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 theGOROOTandPATHin 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
- You can create a file of
.bash_profilein your home directory if the file does not exist - Copy and paste the commands above to the file. Save and close.
- Put
source ~/.bash_profileat the end of the~/.bashrcfile.
macOS
- You can create a file of
.zprofilein your home directory if the file does not exist - 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.
Still have the issue? Apply the same thing to GONOSUMDB, to ignore checksum verification, and GONOPROXY, to fetch directly to the repository and not from a proxy.
Already put the Git repository domain in the GOPRIVATE, GONOSUMDB, and GONOPROXY, but still can't read private repositories?
Ensure that the HTTP scheme is secure. You can add the domain to the GOINSECURE, go env -w GOINSECURE="mygitlab.com", if that is not available (not https), and you still want to access it.
Related Articles
- Concurrency in Go (Part 2)
- Deploy a Go App to AWS EC2
- A Simple Blog with Go
- Pemrograman Dasar Menggunakan GoLang
- Go Module