asdf to manage multiple Golang on Mac

Installation

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.8.1

Add asdf to zsh

ZSH & Homebrew

Add asdf.sh to your ~/.zshrc with:

echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc

ZSH & Git

Add the following to ~/.zshrc:

. $HOME/.asdf/asdf.sh

Install plugin dependencies

brew install gpg gawk

Install Golang

Version control config file: ~/.tool-versions

asdf plugin-add golang

asdf install golang 1.17.1
asdf install golang 1.16.8

asdf global golang 1.17.1

Setup Golang environment

GOPATH, GOROOT, PATH

nano ~/.zshrc

export GOROOT=~/.asdf/installs/golang/1.17.1/go
export GOPATH=$(go env GOPATH)
export PATH="$PATH:$(go env GOPATH)/bin"
# export GOBIN=$GOROOT/bin
export CGO_ENABLED=1

Save and restart your terminal

Try to install something useful

# shfmt lint 
go install mvdan.cc/sh/v3/cmd/[email protected]

# Git Semantic Versioning
go get -u github.com/linyows/git-semv/cmd/git-semv

References:

  1. https://gist.github.com/rubencaro/5ce32fb30bbfa70e7db6be14cf42a35c
  2. https://gist.github.com/rubencaro/5ce32fb30bbfa70e7db6be14cf42a35c
  3. https://stackoverflow.com/questions/25216765/gobin-not-set-cannot-run-go-install