Mac Go环境相关配置

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:
      • 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
安装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则下面步骤会正常
  • 检查
    • 查看版本air -v
    • tips:若提示go air cannot find main module, but found .git/config in...则表示需要在项目根目录执行go mod init,原因为:上面已经把god设置为 GO111MODULE=on的模式,在go1.13+版本中强制要求项目为go module模式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值