Setting up Golang with debugging and VS Code on MacOS

This guide is what I did to get Golang setup on my Mac machine.

Assumptions

You use the following:

asdf
zshrc

Install Golang

From asdf-golang

asdf plugin add golang https://github.com/asdf-community/asdf-golang.git
# ins…


This content originally appeared on DEV Community and was authored by Nerdherd

This guide is what I did to get Golang setup on my Mac machine.

Assumptions

You use the following:

  • asdf
  • zshrc

Install Golang

From asdf-golang

asdf plugin add golang https://github.com/asdf-community/asdf-golang.git
# install latest golang version
asdf install golang latest

# set the glboal version for golang to latest
asdf global golang latest

# reshim 
asdf reshim golang

Add GOROOT in your shell's initialisation

GOROOT is the environment variable that specifics the location of the Go installation

Add the following to ~/.zshrc

. ~/.asdf/plugins/golang/set-env.zsh

This will ensure that GOROOT and GOPATH is set each time you run your terminal

  • GOROOT: specifies location of the Go installation directory (e.g. compiler, linker, standard library)
  • GOPATH: specifies location of your workspace. Workspace is a directory hierachy with three directories: src, pkg, bin

You can see what set-env.zsh does:

$ cat ~/.asdf/plugins/golang/set-env.zsh
asdf_update_golang_env() {
  local go_bin_path
  go_bin_path="$(asdf which go 2>/dev/null)"
  if [[ -n "${go_bin_path}" ]]; then
    export GOROOT
    GOROOT="$(dirname "$(dirname "${go_bin_path:A}")")"

    export GOPATH
    GOPATH="$(dirname "${GOROOT:A}")/packages"
  fi
}

autoload -U add-zsh-hook
add-zsh-hook precmd asdf_update_golang_env

Update your currently opened terminal to use latest ~/.zshrc

source ~/.zshrc

Check to see if GOROOT and GOPATH is set

> echo $GOROOT
/Users/username/.asdf/installs/golang/1.22.5/go

> echo $GOPATH
/Users/username/.asdf/installs/golang/1.22.5/packages


This content originally appeared on DEV Community and was authored by Nerdherd


Print Share Comment Cite Upload Translate Updates
APA

Nerdherd | Sciencx (2024-08-09T14:37:21+00:00) Setting up Golang with debugging and VS Code on MacOS. Retrieved from https://www.scien.cx/2024/08/09/setting-up-golang-with-debugging-and-vs-code-on-macos/

MLA
" » Setting up Golang with debugging and VS Code on MacOS." Nerdherd | Sciencx - Friday August 9, 2024, https://www.scien.cx/2024/08/09/setting-up-golang-with-debugging-and-vs-code-on-macos/
HARVARD
Nerdherd | Sciencx Friday August 9, 2024 » Setting up Golang with debugging and VS Code on MacOS., viewed ,<https://www.scien.cx/2024/08/09/setting-up-golang-with-debugging-and-vs-code-on-macos/>
VANCOUVER
Nerdherd | Sciencx - » Setting up Golang with debugging and VS Code on MacOS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/09/setting-up-golang-with-debugging-and-vs-code-on-macos/
CHICAGO
" » Setting up Golang with debugging and VS Code on MacOS." Nerdherd | Sciencx - Accessed . https://www.scien.cx/2024/08/09/setting-up-golang-with-debugging-and-vs-code-on-macos/
IEEE
" » Setting up Golang with debugging and VS Code on MacOS." Nerdherd | Sciencx [Online]. Available: https://www.scien.cx/2024/08/09/setting-up-golang-with-debugging-and-vs-code-on-macos/. [Accessed: ]
rf:citation
» Setting up Golang with debugging and VS Code on MacOS | Nerdherd | Sciencx | https://www.scien.cx/2024/08/09/setting-up-golang-with-debugging-and-vs-code-on-macos/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.