Protocol Buffers Data Type Go Code Generation: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 23: Line 23:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
   protoc \
   protoc \
    --proto_path=./proto \
     --go_out=./pkg/person \
     --go_out=./pkg/person \
    --proto_path=./proto \
     --go_opt=Mperson.proto=. \
     --go_opt=Mperson.proto=. \
     ./proto/person.proto
     ./proto/person.proto
</syntaxhighlight>
</syntaxhighlight>


The compiler creates a single source file for each <code>.proto</code> file input that is provided on the command line. 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>. The newly created files will be placed under the directory specified by <code>[[#--go_out|--go_out]]</code>, but where exactly depends on the <code>[[#--go_opt|--go_opt]]</code> compiler flags, as shown below. In the example above, a <code>./pkg/person/person.pb.go</code> file will be created.
The compiler creates a single 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>. The newly created files will be placed under the directory specified by <code>[[#--go_out|--go_out]]</code>, but where exactly depends on the <code>[[#--go_opt|--go_opt]]</code> compiler flags, as shown below. In the example above, a <code>./pkg/person/person.pb.go</code> file will be created.


=Arguments=
=Arguments=

Revision as of 17:09, 7 May 2024

External

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

Internal

Overview

The Go code that supports the data types defined in the .proto files is generated withe the compiler is invoked with the go_out flag, as shown below, and the Go plugin is installed.

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

  protoc \
    --proto_path=./proto \
    --go_out=./pkg/person \
    --go_opt=Mperson.proto=. \
    ./proto/person.proto

The compiler creates a single 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. The newly created files will be placed under the directory specified by --go_out, but where exactly depends on the --go_opt compiler flags, as shown below. In the example above, a ./pkg/person/person.pb.go file will be created.

Arguments

--proto_path

The directory where to find the Protocol Buffer files. The individual Protocol Buffer files specified on the command line must be relative to this directory.

--go_out

The directory where the compiler writes the Go output. Where exactly in the directory depends on the value of the --go-opt value.

--go_opt

paths=import

paths=source_relative

module=$PREFIX