2018-05-18 05:49:05 +03:00
|
|
|
# Go support for Protocol Buffers - Google's data interchange format
|
2015-12-07 07:07:14 +03:00
|
|
|
|
2017-06-22 23:25:51 +03:00
|
|
|
[![Build Status](https://travis-ci.org/golang/protobuf.svg?branch=master)](https://travis-ci.org/golang/protobuf)
|
2017-10-21 07:39:52 +03:00
|
|
|
[![GoDoc](https://godoc.org/github.com/golang/protobuf?status.svg)](https://godoc.org/github.com/golang/protobuf)
|
2017-06-22 23:25:51 +03:00
|
|
|
|
2015-12-07 07:07:14 +03:00
|
|
|
Google's data interchange format.
|
2012-11-29 01:51:07 +04:00
|
|
|
Copyright 2010 The Go Authors.
|
2014-11-24 02:28:53 +03:00
|
|
|
https://github.com/golang/protobuf
|
2010-03-21 08:32:34 +03:00
|
|
|
|
2018-11-27 11:56:59 +03:00
|
|
|
This package and the code it generates requires at least Go 1.9.
|
2013-12-26 05:07:52 +04:00
|
|
|
|
2010-03-21 08:32:34 +03:00
|
|
|
This software implements Go bindings for protocol buffers. For
|
|
|
|
information about protocol buffers themselves, see
|
2014-11-24 02:50:43 +03:00
|
|
|
https://developers.google.com/protocol-buffers/
|
2015-10-30 00:51:32 +03:00
|
|
|
|
|
|
|
## Installation ##
|
|
|
|
|
|
|
|
To use this software, you must:
|
|
|
|
- Install the standard C++ implementation of protocol buffers from
|
2014-11-24 02:50:43 +03:00
|
|
|
https://developers.google.com/protocol-buffers/
|
2015-10-30 00:51:32 +03:00
|
|
|
- Of course, install the Go compiler and tools from
|
2014-11-24 02:50:43 +03:00
|
|
|
https://golang.org/
|
2015-10-30 00:51:32 +03:00
|
|
|
See
|
2014-11-24 02:50:43 +03:00
|
|
|
https://golang.org/doc/install
|
2015-10-30 00:51:32 +03:00
|
|
|
for details or, if you are using gccgo, follow the instructions at
|
2014-11-24 02:50:43 +03:00
|
|
|
https://golang.org/doc/install/gccgo
|
2015-10-30 00:51:32 +03:00
|
|
|
- Grab the code from the repository and install the proto package.
|
2017-05-09 19:59:01 +03:00
|
|
|
The simplest way is to run `go get -u github.com/golang/protobuf/protoc-gen-go`.
|
2015-10-30 00:51:32 +03:00
|
|
|
The compiler plugin, protoc-gen-go, will be installed in $GOBIN,
|
|
|
|
defaulting to $GOPATH/bin. It must be in your $PATH for the protocol
|
|
|
|
compiler, protoc, to find it.
|
2010-03-21 08:32:34 +03:00
|
|
|
|
|
|
|
This software has two parts: a 'protocol compiler plugin' that
|
|
|
|
generates Go source files that, once compiled, can access and manage
|
|
|
|
protocol buffers; and a library that implements run-time support for
|
|
|
|
encoding (marshaling), decoding (unmarshaling), and accessing protocol
|
|
|
|
buffers.
|
|
|
|
|
2015-06-16 01:36:59 +03:00
|
|
|
There is support for gRPC in Go using protocol buffers.
|
|
|
|
See the note at the bottom of this file for details.
|
2010-03-21 08:32:34 +03:00
|
|
|
|
|
|
|
There are no insertion points in the plugin.
|
|
|
|
|
|
|
|
|
2015-10-30 00:51:32 +03:00
|
|
|
## Using protocol buffers with Go ##
|
2010-03-21 08:32:34 +03:00
|
|
|
|
|
|
|
Once the software is installed, there are two steps to using it.
|
|
|
|
First you must compile the protocol buffer definitions and then import
|
|
|
|
them, with the support library, into your program.
|
|
|
|
|
2012-11-26 06:58:15 +04:00
|
|
|
To compile the protocol buffer definition, run protoc with the --go_out
|
|
|
|
parameter set to the directory you want to output the Go code to.
|
|
|
|
|
|
|
|
protoc --go_out=. *.proto
|
2010-03-21 08:32:34 +03:00
|
|
|
|
|
|
|
The generated files will be suffixed .pb.go. See the Test code below
|
|
|
|
for an example using such a file.
|
|
|
|
|
2018-03-07 01:57:57 +03:00
|
|
|
## Packages and input paths ##
|
|
|
|
|
|
|
|
The protocol buffer language has a concept of "packages" which does not
|
|
|
|
correspond well to the Go notion of packages. In generated Go code,
|
|
|
|
each source `.proto` file is associated with a single Go package. The
|
|
|
|
name and import path for this package is specified with the `go_package`
|
|
|
|
proto option:
|
|
|
|
|
|
|
|
option go_package = "github.com/golang/protobuf/ptypes/any";
|
|
|
|
|
|
|
|
The protocol buffer compiler will attempt to derive a package name and
|
|
|
|
import path if a `go_package` option is not present, but it is
|
|
|
|
best to always specify one explicitly.
|
|
|
|
|
|
|
|
There is a one-to-one relationship between source `.proto` files and
|
|
|
|
generated `.pb.go` files, but any number of `.pb.go` files may be
|
|
|
|
contained in the same Go package.
|
|
|
|
|
|
|
|
The output name of a generated file is produced by replacing the
|
|
|
|
`.proto` suffix with `.pb.go` (e.g., `foo.proto` produces `foo.pb.go`).
|
|
|
|
However, the output directory is selected in one of two ways. Let
|
|
|
|
us say we have `inputs/x.proto` with a `go_package` option of
|
|
|
|
`github.com/golang/protobuf/p`. The corresponding output file may
|
|
|
|
be:
|
|
|
|
|
|
|
|
- Relative to the import path:
|
|
|
|
|
2018-07-10 22:34:13 +03:00
|
|
|
```shell
|
|
|
|
protoc --go_out=. inputs/x.proto
|
|
|
|
# writes ./github.com/golang/protobuf/p/x.pb.go
|
|
|
|
```
|
2018-03-07 01:57:57 +03:00
|
|
|
|
|
|
|
(This can work well with `--go_out=$GOPATH`.)
|
|
|
|
|
|
|
|
- Relative to the input file:
|
|
|
|
|
2018-07-10 22:34:13 +03:00
|
|
|
```shell
|
|
|
|
protoc --go_out=paths=source_relative:. inputs/x.proto
|
|
|
|
# generate ./inputs/x.pb.go
|
|
|
|
```
|
2018-03-07 01:57:57 +03:00
|
|
|
|
|
|
|
## Generated code ##
|
2010-03-21 08:32:34 +03:00
|
|
|
|
|
|
|
The package comment for the proto library contains text describing
|
|
|
|
the interface provided in Go for protocol buffers. Here is an edited
|
|
|
|
version.
|
|
|
|
|
|
|
|
The proto package converts data structures to and from the
|
|
|
|
wire format of protocol buffers. It works in concert with the
|
|
|
|
Go source code generated for .proto files by the protocol compiler.
|
|
|
|
|
|
|
|
A summary of the properties of the protocol buffer interface
|
|
|
|
for a protocol buffer variable v:
|
|
|
|
|
|
|
|
- Names are turned from camel_case to CamelCase for export.
|
2012-06-28 21:42:01 +04:00
|
|
|
- There are no methods on v to set fields; just treat
|
2010-03-21 08:32:34 +03:00
|
|
|
them as structure fields.
|
2012-06-28 21:42:01 +04:00
|
|
|
- There are getters that return a field's value if set,
|
|
|
|
and return the field's default value if unset.
|
|
|
|
The getters work even if the receiver is a nil message.
|
2010-03-21 08:32:34 +03:00
|
|
|
- The zero value for a struct is its correct initialization state.
|
|
|
|
All desired fields must be set before marshaling.
|
|
|
|
- A Reset() method will restore a protobuf struct to its zero state.
|
|
|
|
- Non-repeated fields are pointers to the values; nil means unset.
|
|
|
|
That is, optional or required field int32 f becomes F *int32.
|
|
|
|
- Repeated fields are slices.
|
2012-06-28 21:42:01 +04:00
|
|
|
- Helper functions are available to aid the setting of fields.
|
|
|
|
Helpers for getting values are superseded by the
|
|
|
|
GetFoo methods and their use is deprecated.
|
|
|
|
msg.Foo = proto.String("hello") // set field
|
2010-03-21 08:32:34 +03:00
|
|
|
- Constants are defined to hold the default values of all fields that
|
|
|
|
have them. They have the form Default_StructName_FieldName.
|
2012-06-28 21:42:01 +04:00
|
|
|
Because the getter methods handle defaulted values,
|
|
|
|
direct use of these constants should be rare.
|
|
|
|
- Enums are given type names and maps from names to values.
|
|
|
|
Enum values are prefixed with the enum's type name. Enum types have
|
|
|
|
a String method, and a Enum method to assist in message construction.
|
2010-03-21 08:32:34 +03:00
|
|
|
- Nested groups and enums have type names prefixed with the name of
|
|
|
|
the surrounding message type.
|
2012-06-28 21:42:01 +04:00
|
|
|
- Extensions are given descriptor names that start with E_,
|
|
|
|
followed by an underscore-delimited list of the nested messages
|
|
|
|
that contain it (if any) followed by the CamelCased name of the
|
|
|
|
extension field itself. HasExtension, ClearExtension, GetExtension
|
|
|
|
and SetExtension are functions for manipulating extensions.
|
2015-09-01 02:45:27 +03:00
|
|
|
- Oneof field sets are given a single field in their message,
|
|
|
|
with distinguished wrapper types for each possible field value.
|
2010-03-21 08:32:34 +03:00
|
|
|
- Marshal and Unmarshal are functions to encode and decode the wire format.
|
|
|
|
|
2016-01-05 03:20:00 +03:00
|
|
|
When the .proto file specifies `syntax="proto3"`, there are some differences:
|
|
|
|
|
|
|
|
- Non-repeated fields of non-message type are values instead of pointers.
|
|
|
|
- Enum types do not get an Enum method.
|
|
|
|
|
2010-03-21 08:32:34 +03:00
|
|
|
Consider file test.proto, containing
|
|
|
|
|
2015-12-07 07:07:14 +03:00
|
|
|
```proto
|
2017-09-02 03:04:52 +03:00
|
|
|
syntax = "proto2";
|
2010-03-21 08:32:34 +03:00
|
|
|
package example;
|
2018-02-22 01:28:13 +03:00
|
|
|
|
2010-03-21 08:32:34 +03:00
|
|
|
enum FOO { X = 17; };
|
2018-02-22 01:28:13 +03:00
|
|
|
|
2010-03-21 08:32:34 +03:00
|
|
|
message Test {
|
|
|
|
required string label = 1;
|
|
|
|
optional int32 type = 2 [default=77];
|
|
|
|
repeated int64 reps = 3;
|
|
|
|
}
|
2015-12-07 07:07:14 +03:00
|
|
|
```
|
2010-03-21 08:32:34 +03:00
|
|
|
|
|
|
|
To create and play with a Test object from the example package,
|
|
|
|
|
2015-12-07 07:07:14 +03:00
|
|
|
```go
|
2010-03-21 08:32:34 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2014-11-24 02:28:53 +03:00
|
|
|
"github.com/golang/protobuf/proto"
|
2010-03-21 08:32:34 +03:00
|
|
|
"path/to/example"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2018-08-05 01:03:15 +03:00
|
|
|
test := &example.Test{
|
2010-03-21 08:32:34 +03:00
|
|
|
Label: proto.String("hello"),
|
2012-06-28 21:42:01 +04:00
|
|
|
Type: proto.Int32(17),
|
2016-01-24 22:11:55 +03:00
|
|
|
Reps: []int64{1, 2, 3},
|
2010-03-21 08:32:34 +03:00
|
|
|
}
|
|
|
|
data, err := proto.Marshal(test)
|
|
|
|
if err != nil {
|
2011-02-12 03:06:41 +03:00
|
|
|
log.Fatal("marshaling error: ", err)
|
2010-03-21 08:32:34 +03:00
|
|
|
}
|
2010-07-29 02:48:19 +04:00
|
|
|
newTest := &example.Test{}
|
2010-03-21 08:32:34 +03:00
|
|
|
err = proto.Unmarshal(data, newTest)
|
|
|
|
if err != nil {
|
2011-02-12 03:06:41 +03:00
|
|
|
log.Fatal("unmarshaling error: ", err)
|
2010-03-21 08:32:34 +03:00
|
|
|
}
|
|
|
|
// Now test and newTest contain the same data.
|
2012-06-28 21:42:01 +04:00
|
|
|
if test.GetLabel() != newTest.GetLabel() {
|
2012-08-07 04:50:51 +04:00
|
|
|
log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
|
2010-03-21 08:32:34 +03:00
|
|
|
}
|
|
|
|
// etc.
|
|
|
|
}
|
2015-12-07 07:07:14 +03:00
|
|
|
```
|
2015-06-16 01:36:59 +03:00
|
|
|
|
2015-12-07 07:02:57 +03:00
|
|
|
## Parameters ##
|
2015-11-25 00:02:47 +03:00
|
|
|
|
|
|
|
To pass extra parameters to the plugin, use a comma-separated
|
|
|
|
parameter list separated from the output directory by a colon:
|
|
|
|
|
|
|
|
protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto
|
|
|
|
|
2018-03-07 01:57:57 +03:00
|
|
|
- `paths=(import | source_relative)` - specifies how the paths of
|
|
|
|
generated files are structured. See the "Packages and imports paths"
|
|
|
|
section above. The default is `import`.
|
2015-11-25 00:02:47 +03:00
|
|
|
- `plugins=plugin1+plugin2` - specifies the list of sub-plugins to
|
|
|
|
load. The only plugin in this repo is `grpc`.
|
|
|
|
- `Mfoo/bar.proto=quux/shme` - declares that foo/bar.proto is
|
|
|
|
associated with Go package quux/shme. This is subject to the
|
|
|
|
import_prefix parameter.
|
|
|
|
|
2018-03-07 01:57:57 +03:00
|
|
|
The following parameters are deprecated and should not be used:
|
|
|
|
|
|
|
|
- `import_prefix=xxx` - a prefix that is added onto the beginning of
|
|
|
|
all imports.
|
|
|
|
- `import_path=foo/bar` - used as the package if no input files
|
|
|
|
declare `go_package`. If it contains slashes, everything up to the
|
|
|
|
rightmost slash is ignored.
|
|
|
|
|
2015-12-07 07:02:57 +03:00
|
|
|
## gRPC Support ##
|
|
|
|
|
2015-06-16 01:36:59 +03:00
|
|
|
If a proto file specifies RPC services, protoc-gen-go can be instructed to
|
|
|
|
generate code compatible with gRPC (http://www.grpc.io/). To do this, pass
|
|
|
|
the `plugins` parameter to protoc-gen-go; the usual way is to insert it into
|
|
|
|
the --go_out argument to protoc:
|
|
|
|
|
|
|
|
protoc --go_out=plugins=grpc:. *.proto
|
2016-03-21 01:39:39 +03:00
|
|
|
|
2016-07-27 20:26:17 +03:00
|
|
|
## Compatibility ##
|
|
|
|
|
|
|
|
The library and the generated code are expected to be stable over time.
|
|
|
|
However, we reserve the right to make breaking changes without notice for the
|
|
|
|
following reasons:
|
|
|
|
|
|
|
|
- Security. A security issue in the specification or implementation may come to
|
|
|
|
light whose resolution requires breaking compatibility. We reserve the right
|
|
|
|
to address such security issues.
|
|
|
|
- Unspecified behavior. There are some aspects of the Protocol Buffers
|
|
|
|
specification that are undefined. Programs that depend on such unspecified
|
|
|
|
behavior may break in future releases.
|
|
|
|
- Specification errors or changes. If it becomes necessary to address an
|
|
|
|
inconsistency, incompleteness, or change in the Protocol Buffers
|
|
|
|
specification, resolving the issue could affect the meaning or legality of
|
|
|
|
existing programs. We reserve the right to address such issues, including
|
|
|
|
updating the implementations.
|
|
|
|
- Bugs. If the library has a bug that violates the specification, a program
|
|
|
|
that depends on the buggy behavior may break if the bug is fixed. We reserve
|
|
|
|
the right to fix such bugs.
|
|
|
|
- Adding methods or fields to generated structs. These may conflict with field
|
|
|
|
names that already exist in a schema, causing applications to break. When the
|
|
|
|
code generator encounters a field in the schema that would collide with a
|
|
|
|
generated field or method name, the code generator will append an underscore
|
|
|
|
to the generated field or method name.
|
|
|
|
- Adding, removing, or changing methods or fields in generated structs that
|
|
|
|
start with `XXX`. These parts of the generated code are exported out of
|
|
|
|
necessity, but should not be considered part of the public API.
|
|
|
|
- Adding, removing, or changing unexported symbols in generated code.
|
|
|
|
|
|
|
|
Any breaking changes outside of these will be announced 6 months in advance to
|
|
|
|
protobuf@googlegroups.com.
|
|
|
|
|
|
|
|
You should, whenever possible, use generated code created by the `protoc-gen-go`
|
|
|
|
tool built at the same commit as the `proto` package. The `proto` package
|
|
|
|
declares package-level constants in the form `ProtoPackageIsVersionX`.
|
|
|
|
Application code and generated code may depend on one of these constants to
|
|
|
|
ensure that compilation will fail if the available version of the proto library
|
|
|
|
is too old. Whenever we make a change to the generated code that requires newer
|
|
|
|
library support, in the same commit we will increment the version number of the
|
|
|
|
generated code and declare a new package-level constant whose name incorporates
|
|
|
|
the latest version number. Removing a compatibility constant is considered a
|
|
|
|
breaking change and would be subject to the announcement policy stated above.
|
2016-03-21 01:39:39 +03:00
|
|
|
|
|
|
|
The `protoc-gen-go/generator` package exposes a plugin interface,
|
|
|
|
which is used by the gRPC code generation. This interface is not
|
|
|
|
supported and is subject to incompatible changes without notice.
|