第一步 下载并安装protobuf
1、下载
git clone https://github.com/protocolbuffers/protobuf.git $GOPATH/src/github.com/protocolbuffers/protobuf
2、 安装依赖
sudo apt-get install autoconf automake libtool curl make g++ unzip libffi-dev -y
3、 部署
3.1
cd protobuf
./autogen.sh
可能会遇到./autogen.sh: 4: ./autogen.sh: autoreconf: not found
解决方法:
sudo apt-get install autoconf automake libtool
3.2
./configure
可能会遇到
configure: error: in `/home/fengge/protobuf’:
configure: error: C++ preprocessor “/lib/cpp” fails sanity check
解决方法
sudo apt-get install build-essential
sudo apt-get install g++
3.3
make
sudo ldconfig
protoc -h
第二步 获取proto包
Go语言的proto API接口
go get -v -u github.com/golang/protobuf/proto
可能会遇到dial tcp 172.217.160.113:443: connectex: A connection attempt failed
解决方法,设置代理
上图这是阿里云代理的介绍。
按照上述步骤,先开启go module功能
注意:开启go module需要go1.11及以上版本
#开启go module功能
export GO111MODULE=on
#把目录切换到项目
#如~/goproject/src/awesomeProject$
go mod init awesomeProject
然后设置阿里云代理
export GOPROXY=https://mirrors.aliyun.com/goproxy/
第三步 安装protoc-gen-go插件
#下载
go get -v -u github.com/golang/protobuf/protoc-gen-go
#到这个插件所在目录
cd $GOPATH/src/github.com/golang/protobuf/protoc-gen-go
go build
#ls 查看是否有protoc-gen-go可执行文件,如果有放到/bin目录下,确保随时可以使用
sudo cp protoc-gen-go /bin