Protocol Buffers Data Type Go Code Generation: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 23: Line 23:


The compiler creates a single Go source file for each <code>.proto</code> command line-provided input file. The name of the output file is the name of the corresponding <code>.proto</code> file where the <code>.proto</code> extension is replaced with <code>.pb.go</code>. <code>person.proto</code> will generate a <code>person.pb.go</code> Go source file.
The compiler creates a single Go source file for each <code>.proto</code> command line-provided input file. The name of the output file is the name of the corresponding <code>.proto</code> file where the <code>.proto</code> extension is replaced with <code>.pb.go</code>. <code>person.proto</code> will generate a <code>person.pb.go</code> Go source file.
The compiler must know Go package's [[Go_Packages#Import_Path|import path]] for every <code>.proto</code> it processes, including those transitively depended upon by the <code>.proto</code> files being processed. There are two ways to specify the Go package import path:
* By declaring it within the <code>.proto</code> file (recommended).
* By declaring it on the command line when invoking <code>protoc</code>.


The newly created Go source files will be placed under the directory specified by <code>--go_out</code>, but where exactly depends on the <code>[[#--go_opt|--go_opt]]</code> compiler flags, as shown below.  
The newly created Go source files will be placed under the directory specified by <code>--go_out</code>, but where exactly depends on the <code>[[#--go_opt|--go_opt]]</code> compiler flags, as shown below.  

Revision as of 18:06, 7 May 2024

External

https://protobuf.dev/reference/go/go-generated

Internal

Overview

Protocol Buffer files are processed by the Protocol Buffer compiler protoc, which needs the Go plugin protoc-gen-go to generate Go code. The plugin is installed as described in the Installation section below. The protoc command line details are discussed in the Code Generation section.

Installation

The generic compiler must be installed with TODO.

To generate Go code, the Go plugin must be installed with:

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

This will install a protoc-gen-go binary in ${GOBIN}.

Code Generation

The compiler requires a mandatory source directory, specified with --proto_path, where it expects the find the .proto Protocol Buffer files, possibly in a directory hierarchy. The Protocol Buffer files are individually specified on the command line, and must be listed as relative to --proto_path.

The compiler creates a single Go source file for each .proto command line-provided input file. The name of the output file is the name of the corresponding .proto file where the .proto extension is replaced with .pb.go. person.proto will generate a person.pb.go Go source file.

The compiler must know Go package's import path for every .proto it processes, including those transitively depended upon by the .proto files being processed. There are two ways to specify the Go package import path:

  • By declaring it within the .proto file (recommended).
  • By declaring it on the command line when invoking protoc.

The newly created Go source files will be placed under the directory specified by --go_out, but where exactly depends on the --go_opt compiler flags, as shown below.

  protoc \
    --proto_path=<protocol-buffer-root-source-directory> \
    --go_out=<protocol-buffer-generated-code-root-directory> \
    --go_opt=... \
    <protocol-buffer-file-1>.proto <protocol-buffer-file-1>.proto ...

For more details on a typical Go project layout, see:

Go Project Layout

Example

A typical layout that includes Protocol Buffer files that will generate code in different packages:

...

--go_opt

paths=import

paths=source_relative

module=$PREFIX