本指南是我在 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
立即学习“go语言免费学习笔记(深入)”;
- 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