本指南是我在 Mac 机器上安装 Golang 时所做的。
您使用以下内容:
来自 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
GOROOT 是指定 Go 安装位置的环境变量
将以下内容添加到 ~/.zshrc
. ~/.asdf/plugins/golang/set-env.zsh
这将确保每次运行终端时都设置 GOROOT 和 GOPATH
你可以看看 set-env.zsh 做了什么:
$ 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
更新当前打开的终端以使用最新的 ~/.zshrc
source ~/.zshrc
检查 GOROOT 和 GOPATH 是否设置
> echo $GOROOT /Users/username/.asdf/installs/golang/1.22.5/go > echo $GOPATH /Users/username/.asdf/installs/golang/1.22.5/packages
以上是在 MacOS 上安装 Golang的详细内容。更多信息请关注PHP中文网其他相关文章!