zzh@ZZHPC:~$ sudo apt install -y protobuf-compiler
zzh@ZZHPC:/zdata/Github/zimplebank$ protoc --version
libprotoc 3.12.4
This version is too old.
zzh@ZZHPC:~$ sudo apt remove protobuf-compiler
Warning
Check the version of protoc
(as indicated below) after installation to ensure that it is sufficiently recent. The versions of protoc
installed by some package managers can be quite dated.
Installing from pre-compiled binaries, as indicated in the next section, is the best way to ensure that you’re using the latest release of protoc
.
Install pre-compiled binaries (any OS)
To install the latest release of the protocol compiler from pre-compiled binaries, follow these instructions:
-
Manually download from github.com/google/protobuf/releases the zip file corresponding to your operating system and computer architecture (
protoc-<version>-<os>-<arch>.zip
), or fetch the file using commands such as the following:$ PB_REL="https://github.com/protocolbuffers/protobuf/releases" $ curl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip
-L, --location (HTTP) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place. -O, --remote-name Write output to a local file named like the remote file we get. (Only the file part of the remote file is used, the path is cut off.)
-
Unzip the file under
$HOME/.local
or a directory of your choice. For example:$ unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local
-
Update your environment’s path variable to include the path to the
protoc
executable. For example:$ export PATH="$PATH:$HOME/.local/bin"
zzh@ZZHPC:~$ protoc --version libprotoc 25.1
To generate source code from .proto files, first install protoc, the protocol buffer compiler (https://grpc.io/docs/protoc-installation/). Then install two more modules to help protoc generate source code specific to the Go language:
zzh@ZZHPC:~$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go: downloading google.golang.org/protobuf v1.32.0 zzh@ZZHPC:~$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest go: downloading google.golang.org/grpc v1.60.1 zzh@ZZHPC:~$ protoc-gen-go --version protoc-gen-go v1.32.0 zzh@ZZHPC:~$ protoc-gen-go-grpc --version protoc-gen-go-grpc 1.3.0
Compiler Invocation
The protocol buffer compiler requires a plugin to generate Go code. Install it using Go 1.16 or higher by running:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
This will install a protoc-gen-go
binary in $GOBIN
. Set the $GOBIN
environment variable to change the installation location. It must be in your $PATH
for the protocol buffer compiler to find it.
The protocol buffer compiler produces Go output when invoked with the go_out
flag. The argument to the go_out
flag is the directory where you want the compiler to write your Go output. The compiler creates a single source file for each .proto
file input. The name of the output file is created by replacing the .proto
extension with .pb.go
.
Where in the output directory the generated .pb.go
file is placed depends on the compiler flags. There are several output modes:
- If the
paths=import
flag is specified, the output file is placed in a directory named after the Go package’s import path. For example, an input fileprotos/buzz.proto
with a Go import path ofexample.com/project/protos/fizz
results in an output file atexample.com/project/protos/fizz/buzz.pb.go
. This is the default output mode if apaths
flag is not specified. - If the
module=$PREFIX
flag is specified, the output file is placed in a directory named after the Go package’s import path, but with the specified directory prefix removed from the output filename. For example, an input fileprotos/buzz.proto
with a Go import path ofexample.com/project/protos/fizz
andexample.com/project
specified as themodule
prefix results in an output file atprotos/fizz/buzz.pb.go
. Generating any Go packages outside the module path results in an error. This mode is useful for outputting generated files directly into a Go module. - If the
paths=source_relative
flag is specified, the output file is placed in the same relative directory as the input file. For example, an input fileprotos/buzz.proto
results in an output file atprotos/buzz.pb.go
.
Flags specific to protoc-gen-go
are provided by passing a go_opt
flag when invoking protoc
. Multiple go_opt
flags may be passed. For example, when running:
protoc --proto_path=src --go_out=out --go_opt=paths=source_relative foo.proto bar/baz.proto
the compiler will read input files foo.proto
and bar/baz.proto
from within the src
directory, and write output files foo.pb.go
and bar/baz.pb.go
to the out
directory. The compiler automatically creates nested output sub-directories if necessary, but will not create the output directory itself.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律