go-zero protoc & protoc-gen-go安裝

2022-04-25 17:47 更新

前言

protoc是一款用C++編寫的工具,其可以將proto文件翻譯為指定語言的代碼。在go-zero的微服務(wù)中,我們采用grpc進(jìn)行服務(wù)間的通信,而grpc的編寫就需要用到protoc和翻譯成go語言rpc stub代碼的插件protoc-gen-go。

方式一:goctl一鍵安裝

$ goctl env check -i -f --verbose                                 
[goctl-env]: preparing to check env

[goctl-env]: looking up "protoc"
[goctl-env]: "protoc" is not found in PATH
[goctl-env]: preparing to install "protoc"
"protoc" installed from cache
[goctl-env]: "protoc" is already installed in "/Users/keson/go/bin/protoc"

[goctl-env]: looking up "protoc-gen-go"
[goctl-env]: "protoc-gen-go" is not found in PATH
[goctl-env]: preparing to install "protoc-gen-go"
"protoc-gen-go" installed from cache
[goctl-env]: "protoc-gen-go" is already installed in "/Users/keson/go/bin/protoc-gen-go"

[goctl-env]: looking up "protoc-gen-go-grpc"
[goctl-env]: "protoc-gen-go-grpc" is not found in PATH
[goctl-env]: preparing to install "protoc-gen-go-grpc"
"protoc-gen-go-grpc" installed from cache
[goctl-env]: "protoc-gen-go-grpc" is already installed in "/Users/keson/go/bin/protoc-gen-go-grpc"

[goctl-env]: congratulations! your goctl environment is ready!

方式二: 源文件安裝

protoc安裝

  • 進(jìn)入protobuf release 頁面,選擇適合自己操作系統(tǒng)的壓縮包文件
  • 解壓?protoc-x.x.x-osx-x86_64.zip?并進(jìn)入?protoc-x.x.x-osx-x86_64?

  $ cd protoc-x.x.x-osx-x86_64/bin

  • 將啟動(dòng)的protoc二進(jìn)制文件移動(dòng)到被添加到環(huán)境變量的任意path下,如$GOPATH/bin,這里不建議直接將其和系統(tǒng)的以下path放在一起。

  $ mv protoc $GOPATH/bin
 Tip
$GOPATH為你本機(jī)的實(shí)際文件夾地址

  • 驗(yàn)證安裝結(jié)果

  $ protoc --version
  libprotoc x.x.x

protoc-gen-go/protoc-gen-go-grpc 安裝

  • 下載安裝protoc-gen-go

  $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
  $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)