golang开发过程中,肯定离不开和protobuf打交道,在mac os当中,搭建好环境,很有必要。
- protobuf
brew install protobuf
- 安装go-protoc-gen
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
项目里使用的protoc版本是3.20.x,protobuf仓库的release页面里可能有些版本没有放出来可以直接用的release包,为了和团队保持一致,需要自己安装好指定版本。
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git checkout 3.20.x
git submodule update --init --recursive
./autogen.sh
./configure
make -j$(nproc) # $(nproc) ensures it uses all cores for compilation
make check
sudo make install
使用的时候可能会提示
could not import google.golang.org/protobuf/runtime/protoimpl (cannot find package "google.golang.org/protobuf/runtime/protoimpl" in any of:
/Users/orange/.goenv/versions/1.16.15/src/google.golang.org/protobuf/runtime/protoimpl (from $GOROOT)
/Users/orange/gopath/src/google.golang.org/protobuf/runtime/protoimpl (from $GOPATH))
通常已经安装好了环境的,但是仍然会出现这个问题,仔细调查发现,对应的protobuf/runtime/protoimpl其实已经下载好了,但是没有在src路径,而是在mod路径当中。
解决方法是:
GO111MODULE=off go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
关闭module=on即可。