Logo

Golang在Vscode中的安装环境部署(Mac os)

2025年5月3日 · 514

1.首先安装Brew

首次安装 Homebrew / Linuxbrew 首先,需要确保系统中安装了 bash、git 和 curl,对于 macOS 用户需额外要求安装 Command Line Tools (CLT) for Xcode。 对于 macOS 用户,系统自带 bash、git 和 curl,在命令行输入 xcode-select --install 安装 CLT for Xcode 即可。 安装完成后执行(这里的User根据实际Mac用户名来):

1.1 配置 Shell 环境​​:

​​
echo >> /Users/ye/.zprofile​
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/ye/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)" 

​作用​​:将 Homebrew 的环境变量添加到 .zprofile(Zsh 的配置文件),确保每次启动终端时自动加载 Homebrew。 ​​路径说明​​:/opt/homebrew/bin/brew 是 Apple Silicon Mac(M1/M2 芯片)的默认安装路径,Intel Mac 路径为 /usr/local/bin/brew

1.2 ​​设置 Homebrew 镜像源​​

echo '# Set non-default Git remotes for Homebrew/brew and Homebrew/homebrew-core.' >> /Users/ye/.zprofile
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> /Users/ye/.zprofile
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> /Users/ye/.zprofile
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"

​​作用​​:通过环境变量 HOMEBREW_BREW_GIT_REMOTE 和 HOMEBREW_CORE_GIT_REMOTE 指定 Homebrew 及核心仓库的镜像源。 ​立即生效​​:export 命令会临时设置当前终端的环境变量,而写入 .zprofile 则是永久生效。

2.安装Golang

执行brew install go
安装成功后,执行:
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

​​操作步骤​​: 打开 VS Code,进入扩展市场(快捷键 Ctrl+Shift+X 或 Cmd+Shift+X)。 搜索 ​​Go​​(由 Go Team at Google 发布)。

golang安装 确认已安装,若未安装则点击 ​​Install​​。 如果已安装但有问题,尝试 ​​卸载后重新安装​​。

然后安装Golang的工具: 打开 VS Code 的命令面板(快捷键 Ctrl+Shift+P 或 Cmd+Shift+P)。 输入 Go: Install/Update Tools,选择此命令。 在弹出的列表中勾选 ​​所有工具​​(或至少 gopls 和 go),点击 ​​OK​​。 等待安装完成(需联网,国内可能需要goproxy.cn)。(国内七牛云,放心使用)