本指南是我在 mac 机器上安装 golang 时所做的。
假设
您使用以下内容:
- asdf
- zshrc
安装go语言
来自 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
登录后复制
在 shell 的初始化中添加 goroot
goroot 是指定 go 安装位置的环境变量
将以下内容添加到~/.zshrc
. ~/.asdf/plugins/golang/set-env.zsh
登录后复制
这将确保每次运行终端时都设置 goroot 和 gopath
- goroot:指定go安装目录的位置(例如编译器、链接器、标准库)
- gopath:指定工作空间的位置。 workspace 是一个目录层次结构,包含三个目录:src、pkg、bin
你可以看看 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的详细内容,更多请关注叮当号网其它相关文章!
文章来自互联网,只做分享使用。发布者:周斌,转转请注明出处:https://www.dingdanghao.com/article/704183.html