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/.localor 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
protocexecutable. 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=importflag is specified, the output file is placed in a directory named after the Go package’s import path. For example, an input fileprotos/buzz.protowith a Go import path ofexample.com/project/protos/fizzresults in an output file atexample.com/project/protos/fizz/buzz.pb.go. This is the default output mode if apathsflag is not specified. - If the
module=$PREFIXflag 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.protowith a Go import path ofexample.com/project/protos/fizzandexample.com/projectspecified as themoduleprefix 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_relativeflag is specified, the output file is placed in the same relative directory as the input file. For example, an input fileprotos/buzz.protoresults 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.

浙公网安备 33010602011771号