Go安装
方式有两种:
tips:需注意,本人主机的 终端为zsh,如果你的是bash需自己摸索或留言一起探讨。
一、下载安装
- 网址:点击进入下载地址
- 运行进行安装后在终端执行
go version
可查看版本信息,表示安装成功
二、Homebrew 安装(推荐)
首先更换brew的源
- 阿里源:
- 替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
- 替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
- 更新brew:
brew update
- 替换homebrew-bottles并使生效:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
- 替换brew.git:
- 清华源:
- 替换brew.git:
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
- 替换homebrew-core.git:
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
- 更新brew:
brew update
- 替换homebrew-bottles并使生效:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
- 替换brew.git:
安装go
- 终端执行:
brew install go
- 执行
go version
即可查看版本信息
环境配置
前置条件
- 一般mac安装的go默认path为
'你的用户名(~)/go'
目录,若没有则需去创建,创建命令:mkdir ~/go
- 还需在创建好的
go
目录下创建src
目录,用于存放源代码
配置通用环境(以便于使用go get)
-
启用 Go Modules 功能:
go env -w GO111MODULE=on
-
配置 GOPROXY 环境变量,以下三选一:
- 七牛云
go env -w GOPROXY=https://goproxy.cn,direct
- 阿里云
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
- 官方
go env -w GOPROXY=https://goproxy.io,direct
- 七牛云
-
查看确认代理:
go env | grep GOPROXY
本地如果有模块缓存,可以使用命令清空
go clean --modcache
配置GOPATH
- 打开zsh配置文件:
vim ~/.zshrc
- 添加代码:
GOPATH="/Users/gabbymrh/go"
,注意:此处请使用你实际的gopath,实行命令可查看go env
扩展安装
由于brew安装的go自带godoc,此处忽略godoc安装教程
Go Air
用于代码更改后的热重载
确保按照前置教程安装好了环境
tips: mac中which命令可以查找程序,比如which air
- 安装
go get -u github.com/cosmtrek/air
- 配置
- 打开zshrc配置:
vim ~/.zshrc
- 在最后添加:
alias air="$GOPATH/bin/air"
,注意:此处可前去GOPATH下的bin目录查看,若有air则下面步骤会正常
- 打开zshrc配置:
- 检查
- 查看版本
air -v
- tips:若提示
go air cannot find main module, but found .git/config in...
则表示需要在项目根目录执行go mod init
,原因为:上面已经把god设置为GO111MODULE=on
的模式,在go1.13+版本中强制要求项目为go module模式
- 查看版本