I had a lot of issues trying to install Golang on macOS using asdf package manager to develop on Visual Studio Code.
So here's the steps needed to setup it properly:
Open Terminal and install asdf with this command:
You have to install Homebrew before running the installation command.
brew install asdf
Don't forget to setup asdf on your shell by following asdf documentation.
After it finishes, it's time to install Go:
asdf install go 1.20.2
asdf global golang 1.20.2
asdf reshim golang 1.20.2
mkdir -p ~/go/{bin,pkg,src}
If you get an error message, remove the folder with this command:
sudo rm -rf ~/go
Re-run the command above to create package and source folders. Note: You have to run it as
sudo
because Go changes this folder permissions.
Add this line to your shell config file:
ZSH users: file is located at
~/.zshrc
Bash users: file is located at~/.zshrc
export GOROOT=~/.asdf/installs/golang/1.20.2/go
If you're using ZSH run this command on Terminal:
source ~/.zshrc
If you're using Bash run this command on Terminal:
source ~/.bashrc
Press Ctrl(Cmd) + Shift + P
type User Settings bash
and hit ENTER.
On this window paste the code down below and don't forget to Save:
"go.toolsGopath": "~/go/",
"go.gopath": "~/go/",
Open this URL and hit Install
.
Press Ctrl(Cmd) + Shift + P
type go install tools
and hit ENTER.
After you get a message like:
All tools successfully installed. You are ready to Go. :)
Reopen Visual Studio Code and everything should be working flawlessly.
Thanks for this @felipemeamaral! ❤️
A small item of feedback if you don't mind: the
GOROOT
env var can be updated using a script provided by theasdf
golang
plugin. So instead of specifying the static version ofgolang
for theGOROOT
variable like you did, you can match the version of thegolang
you're running by referencing this script. Check out this part of their readme here. You can verify this using:echo $GOROOT
which should return something like/Users/username/.asdf/installs/golang/1.21.4/go
; andwhich go
which should return/Users/username/.asdf/shims/go
As you have indicated, you do still have to reload your config every time still to make sure the switch happens :)Update: You shouldn't need to reload your shell after setting this up. Re-shimming after an install/version pin may be required though.
An aside: I personally encountered a weird issue with
vscode-go
where its tools and binaries where being placed into the root of my projects and it had to do with it not being able to reconcile the version ofgo
used partly because of theGOROOT
env var, but also becausehomebrew
installed a version ofgo
to go along withgolangci-lint
which was interfering with whichgo
install to use. Althoughgolangci-lint
recommend you use their pre-made binaries to avoid issues, you can use their docker image as an alternative. This is depicted in this commit for a personal project.