1 # Go support for Protocol Buffers
3 [![Build Status](https://travis-ci.org/golang/protobuf.svg?branch=master)](https://travis-ci.org/golang/protobuf)
4 [![GoDoc](https://godoc.org/github.com/golang/protobuf?status.svg)](https://godoc.org/github.com/golang/protobuf)
6 Google's data interchange format.
7 Copyright 2010 The Go Authors.
8 https://github.com/golang/protobuf
10 This package and the code it generates requires at least Go 1.4.
12 This software implements Go bindings for protocol buffers. For
13 information about protocol buffers themselves, see
14 https://developers.google.com/protocol-buffers/
18 To use this software, you must:
19 - Install the standard C++ implementation of protocol buffers from
20 https://developers.google.com/protocol-buffers/
21 - Of course, install the Go compiler and tools from
24 https://golang.org/doc/install
25 for details or, if you are using gccgo, follow the instructions at
26 https://golang.org/doc/install/gccgo
27 - Grab the code from the repository and install the proto package.
28 The simplest way is to run `go get -u github.com/golang/protobuf/protoc-gen-go`.
29 The compiler plugin, protoc-gen-go, will be installed in $GOBIN,
30 defaulting to $GOPATH/bin. It must be in your $PATH for the protocol
31 compiler, protoc, to find it.
33 This software has two parts: a 'protocol compiler plugin' that
34 generates Go source files that, once compiled, can access and manage
35 protocol buffers; and a library that implements run-time support for
36 encoding (marshaling), decoding (unmarshaling), and accessing protocol
39 There is support for gRPC in Go using protocol buffers.
40 See the note at the bottom of this file for details.
42 There are no insertion points in the plugin.
45 ## Using protocol buffers with Go ##
47 Once the software is installed, there are two steps to using it.
48 First you must compile the protocol buffer definitions and then import
49 them, with the support library, into your program.
51 To compile the protocol buffer definition, run protoc with the --go_out
52 parameter set to the directory you want to output the Go code to.
54 protoc --go_out=. *.proto
56 The generated files will be suffixed .pb.go. See the Test code below
57 for an example using such a file.
60 The package comment for the proto library contains text describing
61 the interface provided in Go for protocol buffers. Here is an edited
66 The proto package converts data structures to and from the
67 wire format of protocol buffers. It works in concert with the
68 Go source code generated for .proto files by the protocol compiler.
70 A summary of the properties of the protocol buffer interface
71 for a protocol buffer variable v:
73 - Names are turned from camel_case to CamelCase for export.
74 - There are no methods on v to set fields; just treat
75 them as structure fields.
76 - There are getters that return a field's value if set,
77 and return the field's default value if unset.
78 The getters work even if the receiver is a nil message.
79 - The zero value for a struct is its correct initialization state.
80 All desired fields must be set before marshaling.
81 - A Reset() method will restore a protobuf struct to its zero state.
82 - Non-repeated fields are pointers to the values; nil means unset.
83 That is, optional or required field int32 f becomes F *int32.
84 - Repeated fields are slices.
85 - Helper functions are available to aid the setting of fields.
86 Helpers for getting values are superseded by the
87 GetFoo methods and their use is deprecated.
88 msg.Foo = proto.String("hello") // set field
89 - Constants are defined to hold the default values of all fields that
90 have them. They have the form Default_StructName_FieldName.
91 Because the getter methods handle defaulted values,
92 direct use of these constants should be rare.
93 - Enums are given type names and maps from names to values.
94 Enum values are prefixed with the enum's type name. Enum types have
95 a String method, and a Enum method to assist in message construction.
96 - Nested groups and enums have type names prefixed with the name of
97 the surrounding message type.
98 - Extensions are given descriptor names that start with E_,
99 followed by an underscore-delimited list of the nested messages
100 that contain it (if any) followed by the CamelCased name of the
101 extension field itself. HasExtension, ClearExtension, GetExtension
102 and SetExtension are functions for manipulating extensions.
103 - Oneof field sets are given a single field in their message,
104 with distinguished wrapper types for each possible field value.
105 - Marshal and Unmarshal are functions to encode and decode the wire format.
107 When the .proto file specifies `syntax="proto3"`, there are some differences:
109 - Non-repeated fields of non-message type are values instead of pointers.
110 - Enum types do not get an Enum method.
112 Consider file test.proto, containing
118 enum FOO { X = 17; };
121 required string label = 1;
122 optional int32 type = 2 [default=77];
123 repeated int64 reps = 3;
124 optional group OptionalGroup = 4 {
125 required string RequiredField = 5;
130 To create and play with a Test object from the example package,
138 "github.com/golang/protobuf/proto"
143 test := &example.Test {
144 Label: proto.String("hello"),
145 Type: proto.Int32(17),
146 Reps: []int64{1, 2, 3},
147 Optionalgroup: &example.Test_OptionalGroup {
148 RequiredField: proto.String("good bye"),
151 data, err := proto.Marshal(test)
153 log.Fatal("marshaling error: ", err)
155 newTest := &example.Test{}
156 err = proto.Unmarshal(data, newTest)
158 log.Fatal("unmarshaling error: ", err)
160 // Now test and newTest contain the same data.
161 if test.GetLabel() != newTest.GetLabel() {
162 log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
170 To pass extra parameters to the plugin, use a comma-separated
171 parameter list separated from the output directory by a colon:
174 protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto
177 - `import_prefix=xxx` - a prefix that is added onto the beginning of
178 all imports. Useful for things like generating protos in a
179 subdirectory, or regenerating vendored protobufs in-place.
180 - `import_path=foo/bar` - used as the package if no input files
181 declare `go_package`. If it contains slashes, everything up to the
182 rightmost slash is ignored.
183 - `plugins=plugin1+plugin2` - specifies the list of sub-plugins to
184 load. The only plugin in this repo is `grpc`.
185 - `Mfoo/bar.proto=quux/shme` - declares that foo/bar.proto is
186 associated with Go package quux/shme. This is subject to the
187 import_prefix parameter.
191 If a proto file specifies RPC services, protoc-gen-go can be instructed to
192 generate code compatible with gRPC (http://www.grpc.io/). To do this, pass
193 the `plugins` parameter to protoc-gen-go; the usual way is to insert it into
194 the --go_out argument to protoc:
196 protoc --go_out=plugins=grpc:. *.proto
200 The library and the generated code are expected to be stable over time.
201 However, we reserve the right to make breaking changes without notice for the
204 - Security. A security issue in the specification or implementation may come to
205 light whose resolution requires breaking compatibility. We reserve the right
206 to address such security issues.
207 - Unspecified behavior. There are some aspects of the Protocol Buffers
208 specification that are undefined. Programs that depend on such unspecified
209 behavior may break in future releases.
210 - Specification errors or changes. If it becomes necessary to address an
211 inconsistency, incompleteness, or change in the Protocol Buffers
212 specification, resolving the issue could affect the meaning or legality of
213 existing programs. We reserve the right to address such issues, including
214 updating the implementations.
215 - Bugs. If the library has a bug that violates the specification, a program
216 that depends on the buggy behavior may break if the bug is fixed. We reserve
217 the right to fix such bugs.
218 - Adding methods or fields to generated structs. These may conflict with field
219 names that already exist in a schema, causing applications to break. When the
220 code generator encounters a field in the schema that would collide with a
221 generated field or method name, the code generator will append an underscore
222 to the generated field or method name.
223 - Adding, removing, or changing methods or fields in generated structs that
224 start with `XXX`. These parts of the generated code are exported out of
225 necessity, but should not be considered part of the public API.
226 - Adding, removing, or changing unexported symbols in generated code.
228 Any breaking changes outside of these will be announced 6 months in advance to
229 protobuf@googlegroups.com.
231 You should, whenever possible, use generated code created by the `protoc-gen-go`
232 tool built at the same commit as the `proto` package. The `proto` package
233 declares package-level constants in the form `ProtoPackageIsVersionX`.
234 Application code and generated code may depend on one of these constants to
235 ensure that compilation will fail if the available version of the proto library
236 is too old. Whenever we make a change to the generated code that requires newer
237 library support, in the same commit we will increment the version number of the
238 generated code and declare a new package-level constant whose name incorporates
239 the latest version number. Removing a compatibility constant is considered a
240 breaking change and would be subject to the announcement policy stated above.
242 The `protoc-gen-go/generator` package exposes a plugin interface,
243 which is used by the gRPC code generation. This interface is not
244 supported and is subject to incompatible changes without notice.