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

Go官网 golang.org

Go语言中文网 studygolang

.bash_profile

##brew更换export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles###golang env#golang proxyexport GOPROXY=https://mirrors.aliyun.com/goproxy/#GOPATHexport GOPATH=$HOME/golang-project#protoc-gen-go, protoc-gen-grpc-gateway, protoc-gen-swaggerexport PATH=$PATH:/usr/local/opt/go/libexec/bin:$GOPATH/bin`#go modexport GO111MODULE=on

protobuf相干装置

次要有

  1. protoc
  2. protoc-gen-go
  3. protoc-gen-grpc-gateway
  4. protoc-gen-swagger

装置办法如下:

brew install protocgo install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gatewaygo install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swaggergo 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 -eset -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