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
sdk
folder in your home directory,~/sdk
./home/your-username/sdk
for 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.5
to thePATH
in 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@latest
go1.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 theGOROOT
andPATH
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
- You can create a file of
.bash_profile
in your home directory if the file does not exist - Copy and paste the commands above to the file. Save and close.
- Put
source ~/.bash_profile
at the end of the~/.bashrc
file.
macOS
- You can create a file of
.zprofile
in 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
.
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