关于macos:MacOS-Go开发环境配置

44次阅读

共计 2676 个字符,预计需要花费 7 分钟才能阅读完成。

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 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 相干装置

次要有

  1. protoc
  2. protoc-gen-go
  3. protoc-gen-grpc-gateway
  4. 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

正文完
 0