grpc根據proto文件自動生成go源碼
安裝protoc工具
去https://github.com/protocolbuffers/protobuf/releases下載protobuf的編譯器protoc,windows上可以直接下到exe文件(linux則需要編譯),最后將下載好的可執行文件拷貝到$GOPATH的bin目錄下($GOPATH/bin目錄最好添加到系統環境變量里)
linux環境,下載protoc源碼,下載地址,我選擇如圖的安裝包,可以不需要再編譯,直接將bin添加到環境變量中或者將bin/protoc 軟鏈接到/usr/bin下。
提示:官方或非官方的protobuf,二選一即可,都下載也不會沖突。
下載官方protobuf插件
git clone https://github.com/protocolbuffers/protobuf-go.git ./google.golang.org/protobuf
下載非官方gogoprotobuf插件
go
get
github.com/gogo/protobuf/protoc-gen-gofast
官方的生成protoc-gen-go.exe
go install google.golang.org/protobuf/cmd/protoc-gen-go/
根據proto文件自動生成test.pb.go
protoc --go_out=plugins=grpc:.\ .\test.proto
protoc --gofast_out=plugins=grpc:./ ./test.proto