m1 mac 配置 vscode go 开发环境

安装 go

go下载链接:https://go.dev/dl/

image-20250112145142114

下载之后本地安装就可以了。

安装之后,go root 目录一般在系统的 /usr/local/go

配置 go path

# 这里用zsh举例
vim ~/.zshrc

# go path 配置
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

# 生效配置
source ~/.zshrc

# 执行go env 查看配置是否生效
go env

安装 go 组件

# 配置 go 加速
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

# 配置 go sumdb
go env -w GOSUMDB="sum.golang.org"

# 安装组件
go install -v golang.org/x/tools/gopls@latest
go install -v github.com/ramya-rao-a/go-outline@latest
go install -v github.com/josharian/impl@latest
go install -v github.com/fatih/gomodifytags@latest
go install -v github.com/ramya-rao-a/go-outline@latest
go install -v github.com/josharian/impl@latest
go install -v github.com/fatih/gomodifytags@latest
go install -v golang.org/x/tools/gopls@latest
go install -v github.com/mdempsky/gocode@latest
go install -v github.com/cweill/gotests/gotests@v1.6.0
go install -v github.com/haya14busa/goplay/cmd/goplay@v1.0.0
go install -v github.com/go-delve/delve/cmd/dlv@latest
go install -v golang.org/x/tools/cmd/goimports@latest

vscode 配置

安装组件

code runner, go

image-20250112150253870

配置 setting

点击右下角齿轮 -> 选择“Settings”:

image-20250112150428132

点击 Extensions -> Go -> “Edit in settings.json”

image-20250112150517649

在 settings.json 中添加 2 行代码,并保存

"code-runner.runInTerminal": true,
"go.goroot": "/usr/local/go"

image-20250112150552412

编写脚本运行测试

编写完脚本,点击右上角的执行按钮即可

image-20250112150713547