MacOS Go开发环境配置
Homebrew
安装Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
脚本很可能会停在
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
具体的解决方法可以参考:
https://juejin.im/post/6844903782589923335
之后下载go
make
等工具时,会出现下载龟速的情况,因为homebrew
默认的是国外的源,下载的很慢,所以需要更换源为国内的
# 替换brew.git:
$ cd "$(brew --repo)"
# 中国科大:
$ git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# 清华大学:
$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
# 替换homebrew-core.git:
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
# 中国科大:
$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# 清华大学:
$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
# 替换homebrew-bottles:
# 中国科大:
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
$ source ~/.bash_profile
# 清华大学:
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
$ source ~/.bash_profile
# 应用生效:
$ brew update
Go安装
可以直接通过brew命令工具安装,或者在golang官网下载安装。
brew install go
.bash_profile
##brew更换
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
###golang env
#golang proxy
export GOPROXY=https://mirrors.aliyun.com/goproxy/
#GOPATH
export GOPATH=$HOME/golang-project
#protoc-gen-go, protoc-gen-grpc-gateway, protoc-gen-swagger
export PATH=$PATH:/usr/local/opt/go/libexec/bin:$GOPATH/bin`
#go mod
export GO111MODULE=on
protobuf相关安装
主要有
protoc
protoc-gen-go
protoc-gen-grpc-gateway
protoc-gen-swagger
安装方法如下:
brew install protoc
go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go install github.com/golang/protobuf/protoc-gen-go
安装后protoc-gen-go, protoc-gen-grpc-gateway, protoc-gen-swagger
的路径是$GOPATH/bin
,需要将它们加入到PATH
中,比如加入到.bash_profile
中,添加下面这行
export PATH=$PATH:/usr/local/opt/go/libexec/bin:$GOPATH/bin
使用示例:
根据proto
生成pb代码,网关api代码,swagger文档
protoc
使用方法:protoc -h
生成stub, gw, swagge
文档:https://github.com/grpc-ecosystem/grpc-gateway
#!/bin/bash
set -e
set -x
GEN_PATH=./
PROTOS=`find . -name "*.proto" | awk -F './' '{print $2}'`
echo ${PROTOS}
for i in ${PROTOS}; do
echo $i;
#生成platform.pb.go
protoc -I/usr/local/include --proto_path=$GOPATH/src:. --proto_path=$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--go_out=plugins=grpc,paths=source_relative,:${GEN_PATH} $i;
#生成platform.pb.gw.go, platform.swagger.json
protoc -I/usr/local/include --proto_path=$GOPATH/src:. --proto_path=$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--swagger_out=logtostderr=true:. --grpc-gateway_out=logtostderr=true:${GEN_PATH} $i;
done
常见问题
gocode-gomod安装失败
如果在上述安装插件的过程中出现gocode-gomod
问题,解决办法如下
按照之前设置go
代理之后,开启go module
export GO111MODULE=on
# 下载gocode
go get -u github.com/stamblerre/gocode
# 下载完成后进入`GOPATH`中的`bin`文件中修改gocode
mv gocode gocode-gomod
# 再次下载gocode
go get -u github.com/mdempsky/gocode
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。