|
1 |
| -# Go support for Protocol Buffers - Google's data interchange format |
| 1 | +# Go support for Protocol Buffers |
2 | 2 |
|
| 3 | +[](https://pkg.go.dev/mod/github.com/golang/protobuf) |
3 | 4 | [](https://travis-ci.org/golang/protobuf)
|
4 |
| -[](https://godoc.org/github.com/golang/protobuf) |
5 | 5 |
|
6 |
| -Google's data interchange format. |
7 |
| -Copyright 2010 The Go Authors. |
8 |
| -https://github.com/golang/protobuf |
9 |
| - |
10 |
| -This package and the code it generates requires at least Go 1.9. |
11 |
| - |
12 |
| -This software implements Go bindings for protocol buffers. For |
13 |
| -information about protocol buffers themselves, see |
14 |
| - https://developers.google.com/protocol-buffers/ |
15 |
| - |
16 |
| -## Installation ## |
17 |
| - |
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 |
22 |
| - https://golang.org/ |
23 |
| - See |
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. |
32 |
| - |
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 |
37 |
| -buffers. |
38 |
| - |
39 |
| -There is support for gRPC in Go using protocol buffers. |
40 |
| -See the note at the bottom of this file for details. |
41 |
| - |
42 |
| -There are no insertion points in the plugin. |
43 |
| - |
44 |
| - |
45 |
| -## Using protocol buffers with Go ## |
46 |
| - |
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. |
50 |
| - |
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. |
53 |
| - |
54 |
| - protoc --go_out=. *.proto |
55 |
| - |
56 |
| -The generated files will be suffixed .pb.go. See the Test code below |
57 |
| -for an example using such a file. |
58 |
| - |
59 |
| -## Packages and input paths ## |
60 |
| - |
61 |
| -The protocol buffer language has a concept of "packages" which does not |
62 |
| -correspond well to the Go notion of packages. In generated Go code, |
63 |
| -each source `.proto` file is associated with a single Go package. The |
64 |
| -name and import path for this package is specified with the `go_package` |
65 |
| -proto option: |
66 |
| - |
67 |
| - option go_package = "github.com/golang/protobuf/ptypes/any"; |
68 |
| - |
69 |
| -The protocol buffer compiler will attempt to derive a package name and |
70 |
| -import path if a `go_package` option is not present, but it is |
71 |
| -best to always specify one explicitly. |
72 |
| - |
73 |
| -There is a one-to-one relationship between source `.proto` files and |
74 |
| -generated `.pb.go` files, but any number of `.pb.go` files may be |
75 |
| -contained in the same Go package. |
76 |
| - |
77 |
| -The output name of a generated file is produced by replacing the |
78 |
| -`.proto` suffix with `.pb.go` (e.g., `foo.proto` produces `foo.pb.go`). |
79 |
| -However, the output directory is selected in one of two ways. Let |
80 |
| -us say we have `inputs/x.proto` with a `go_package` option of |
81 |
| -`github.com/golang/protobuf/p`. The corresponding output file may |
82 |
| -be: |
83 |
| - |
84 |
| -- Relative to the import path: |
85 |
| - |
86 |
| -```shell |
87 |
| - protoc --go_out=. inputs/x.proto |
88 |
| - # writes ./github.com/golang/protobuf/p/x.pb.go |
89 |
| -``` |
90 |
| - |
91 |
| - (This can work well with `--go_out=$GOPATH`.) |
92 |
| - |
93 |
| -- Relative to the input file: |
94 |
| - |
95 |
| -```shell |
96 |
| -protoc --go_out=paths=source_relative:. inputs/x.proto |
97 |
| -# generate ./inputs/x.pb.go |
98 |
| -``` |
99 |
| - |
100 |
| -## Generated code ## |
101 |
| - |
102 |
| -The package comment for the proto library contains text describing |
103 |
| -the interface provided in Go for protocol buffers. Here is an edited |
104 |
| -version. |
105 |
| - |
106 |
| -The proto package converts data structures to and from the |
107 |
| -wire format of protocol buffers. It works in concert with the |
108 |
| -Go source code generated for .proto files by the protocol compiler. |
109 |
| - |
110 |
| -A summary of the properties of the protocol buffer interface |
111 |
| -for a protocol buffer variable v: |
112 |
| - |
113 |
| - - Names are turned from camel_case to CamelCase for export. |
114 |
| - - There are no methods on v to set fields; just treat |
115 |
| - them as structure fields. |
116 |
| - - There are getters that return a field's value if set, |
117 |
| - and return the field's default value if unset. |
118 |
| - The getters work even if the receiver is a nil message. |
119 |
| - - The zero value for a struct is its correct initialization state. |
120 |
| - All desired fields must be set before marshaling. |
121 |
| - - A Reset() method will restore a protobuf struct to its zero state. |
122 |
| - - Non-repeated fields are pointers to the values; nil means unset. |
123 |
| - That is, optional or required field int32 f becomes F *int32. |
124 |
| - - Repeated fields are slices. |
125 |
| - - Helper functions are available to aid the setting of fields. |
126 |
| - Helpers for getting values are superseded by the |
127 |
| - GetFoo methods and their use is deprecated. |
128 |
| - msg.Foo = proto.String("hello") // set field |
129 |
| - - Constants are defined to hold the default values of all fields that |
130 |
| - have them. They have the form Default_StructName_FieldName. |
131 |
| - Because the getter methods handle defaulted values, |
132 |
| - direct use of these constants should be rare. |
133 |
| - - Enums are given type names and maps from names to values. |
134 |
| - Enum values are prefixed with the enum's type name. Enum types have |
135 |
| - a String method, and a Enum method to assist in message construction. |
136 |
| - - Nested groups and enums have type names prefixed with the name of |
137 |
| - the surrounding message type. |
138 |
| - - Extensions are given descriptor names that start with E_, |
139 |
| - followed by an underscore-delimited list of the nested messages |
140 |
| - that contain it (if any) followed by the CamelCased name of the |
141 |
| - extension field itself. HasExtension, ClearExtension, GetExtension |
142 |
| - and SetExtension are functions for manipulating extensions. |
143 |
| - - Oneof field sets are given a single field in their message, |
144 |
| - with distinguished wrapper types for each possible field value. |
145 |
| - - Marshal and Unmarshal are functions to encode and decode the wire format. |
146 |
| - |
147 |
| -When the .proto file specifies `syntax="proto3"`, there are some differences: |
148 |
| - |
149 |
| - - Non-repeated fields of non-message type are values instead of pointers. |
150 |
| - - Enum types do not get an Enum method. |
151 |
| - |
152 |
| -Consider file test.proto, containing |
153 |
| - |
154 |
| -```proto |
155 |
| - syntax = "proto2"; |
156 |
| - package example; |
157 |
| -
|
158 |
| - enum FOO { X = 17; }; |
159 |
| -
|
160 |
| - message Test { |
161 |
| - required string label = 1; |
162 |
| - optional int32 type = 2 [default=77]; |
163 |
| - repeated int64 reps = 3; |
164 |
| - } |
165 |
| -``` |
166 |
| - |
167 |
| -To create and play with a Test object from the example package, |
168 |
| - |
169 |
| -```go |
170 |
| - package main |
171 |
| - |
172 |
| - import ( |
173 |
| - "log" |
174 |
| - |
175 |
| - "github.com/golang/protobuf/proto" |
176 |
| - "path/to/example" |
177 |
| - ) |
178 |
| - |
179 |
| - func main() { |
180 |
| - test := &example.Test{ |
181 |
| - Label: proto.String("hello"), |
182 |
| - Type: proto.Int32(17), |
183 |
| - Reps: []int64{1, 2, 3}, |
184 |
| - } |
185 |
| - data, err := proto.Marshal(test) |
186 |
| - if err != nil { |
187 |
| - log.Fatal("marshaling error: ", err) |
188 |
| - } |
189 |
| - newTest := &example.Test{} |
190 |
| - err = proto.Unmarshal(data, newTest) |
191 |
| - if err != nil { |
192 |
| - log.Fatal("unmarshaling error: ", err) |
193 |
| - } |
194 |
| - // Now test and newTest contain the same data. |
195 |
| - if test.GetLabel() != newTest.GetLabel() { |
196 |
| - log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel()) |
197 |
| - } |
198 |
| - // etc. |
199 |
| - } |
200 |
| -``` |
201 |
| - |
202 |
| -## Parameters ## |
203 |
| - |
204 |
| -To pass extra parameters to the plugin, use a comma-separated |
205 |
| -parameter list separated from the output directory by a colon: |
206 |
| - |
207 |
| - protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto |
208 |
| - |
209 |
| -- `paths=(import | source_relative)` - specifies how the paths of |
210 |
| - generated files are structured. See the "Packages and imports paths" |
211 |
| - section above. The default is `import`. |
212 |
| -- `plugins=plugin1+plugin2` - specifies the list of sub-plugins to |
213 |
| - load. The only plugin in this repo is `grpc`. |
214 |
| -- `Mfoo/bar.proto=quux/shme` - declares that foo/bar.proto is |
215 |
| - associated with Go package quux/shme. This is subject to the |
216 |
| - import_prefix parameter. |
217 |
| - |
218 |
| -The following parameters are deprecated and should not be used: |
219 |
| - |
220 |
| -- `import_prefix=xxx` - a prefix that is added onto the beginning of |
221 |
| - all imports. |
222 |
| -- `import_path=foo/bar` - used as the package if no input files |
223 |
| - declare `go_package`. If it contains slashes, everything up to the |
224 |
| - rightmost slash is ignored. |
225 |
| - |
226 |
| -## gRPC Support ## |
227 |
| - |
228 |
| -If a proto file specifies RPC services, protoc-gen-go can be instructed to |
229 |
| -generate code compatible with gRPC (http://www.grpc.io/). To do this, pass |
230 |
| -the `plugins` parameter to protoc-gen-go; the usual way is to insert it into |
231 |
| -the --go_out argument to protoc: |
232 |
| - |
233 |
| - protoc --go_out=plugins=grpc:. *.proto |
234 |
| - |
235 |
| -## Compatibility ## |
236 |
| - |
237 |
| -The library and the generated code are expected to be stable over time. |
238 |
| -However, we reserve the right to make breaking changes without notice for the |
239 |
| -following reasons: |
240 |
| - |
241 |
| -- Security. A security issue in the specification or implementation may come to |
242 |
| - light whose resolution requires breaking compatibility. We reserve the right |
243 |
| - to address such security issues. |
244 |
| -- Unspecified behavior. There are some aspects of the Protocol Buffers |
245 |
| - specification that are undefined. Programs that depend on such unspecified |
246 |
| - behavior may break in future releases. |
247 |
| -- Specification errors or changes. If it becomes necessary to address an |
248 |
| - inconsistency, incompleteness, or change in the Protocol Buffers |
249 |
| - specification, resolving the issue could affect the meaning or legality of |
250 |
| - existing programs. We reserve the right to address such issues, including |
251 |
| - updating the implementations. |
252 |
| -- Bugs. If the library has a bug that violates the specification, a program |
253 |
| - that depends on the buggy behavior may break if the bug is fixed. We reserve |
254 |
| - the right to fix such bugs. |
255 |
| -- Adding methods or fields to generated structs. These may conflict with field |
256 |
| - names that already exist in a schema, causing applications to break. When the |
257 |
| - code generator encounters a field in the schema that would collide with a |
258 |
| - generated field or method name, the code generator will append an underscore |
259 |
| - to the generated field or method name. |
260 |
| -- Adding, removing, or changing methods or fields in generated structs that |
261 |
| - start with `XXX`. These parts of the generated code are exported out of |
262 |
| - necessity, but should not be considered part of the public API. |
263 |
| -- Adding, removing, or changing unexported symbols in generated code. |
| 6 | +This module |
| 7 | +([`github.com/golang/protobuf`](https://pkg.go.dev/mod/github.com/golang/protobuf)) |
| 8 | +contains Go bindings for protocol buffers. |
| 9 | + |
| 10 | +It has been superseded by the |
| 11 | +[`google.golang.org/protobuf`](https://pkg.go.dev/mod/google.golang.org/protobuf) |
| 12 | +module, which contains an updated and simplified API, |
| 13 | +support for protobuf reflection, and many other improvements. |
| 14 | +We recommend that new code use the `google.golang.org/protobuf` module. |
| 15 | + |
| 16 | +Versions v1.4 and later of `github.com/golang/protobuf` are implemented |
| 17 | +in terms of `google.golang.org/protobuf`. |
| 18 | +Programs which use both modules should use at least version v1.4 of this one. |
| 19 | + |
| 20 | +See |
| 21 | +[release note documentation](https://github.com/golang/protobuf/releases) |
| 22 | +for more information about individual releases of this project. |
| 23 | + |
| 24 | +See |
| 25 | +[documentation for the next major revision](https://pkg.go.dev/mod/google.golang.org/protobuf) |
| 26 | +for more information about the purpose, usage, and history of this project. |
| 27 | + |
| 28 | +## Package index |
| 29 | + |
| 30 | +Summary of the packages provided by this module: |
| 31 | + |
| 32 | +* [`proto`](https://pkg.go.dev/github.com/golang/protobuf/proto): Package |
| 33 | + `proto` provides functions operating on protobuf messages such as cloning, |
| 34 | + merging, and checking equality, as well as binary serialization and text |
| 35 | + serialization. |
| 36 | +* [`jsonpb`](https://pkg.go.dev/github.com/golang/protobuf/jsonpb): Package |
| 37 | + `jsonpb` serializes protobuf messages as JSON. |
| 38 | +* [`ptypes`](https://pkg.go.dev/github.com/golang/protobuf/ptypes): Package |
| 39 | + `ptypes` provides helper functionality for protobuf well-known types. |
| 40 | +* [`ptypes/any`](https://pkg.go.dev/github.com/golang/protobuf/ptypes/any): |
| 41 | + Package `any` is the generated package for `google/protobuf/any.proto`. |
| 42 | +* [`ptypes/empty`](https://pkg.go.dev/github.com/golang/protobuf/ptypes/empty): |
| 43 | + Package `empty` is the generated package for `google/protobuf/empty.proto`. |
| 44 | +* [`ptypes/timestamp`](https://pkg.go.dev/github.com/golang/protobuf/ptypes/timestamp): |
| 45 | + Package `timestamp` is the generated package for |
| 46 | + `google/protobuf/timestamp.proto`. |
| 47 | +* [`ptypes/duration`](https://pkg.go.dev/github.com/golang/protobuf/ptypes/duration): |
| 48 | + Package `duration` is the generated package for |
| 49 | + `google/protobuf/duration.proto`. |
| 50 | +* [`ptypes/wrappers`](https://pkg.go.dev/github.com/golang/protobuf/ptypes/wrappers): |
| 51 | + Package `wrappers` is the generated package for |
| 52 | + `google/protobuf/wrappers.proto`. |
| 53 | +* [`ptypes/struct`](https://pkg.go.dev/github.com/golang/protobuf/ptypes/struct): |
| 54 | + Package `struct` is the generated package for |
| 55 | + `google/protobuf/struct.proto`. |
| 56 | +* [`protoc-gen-go/descriptor`](https://pkg.go.dev/github.com/golang/protobuf/protoc-gen-go/descriptor): |
| 57 | + Package `descriptor` is the generated package for |
| 58 | + `google/protobuf/descriptor.proto`. |
| 59 | +* [`protoc-gen-go/plugin`](https://pkg.go.dev/github.com/golang/protobuf/protoc-gen-go/plugin): |
| 60 | + Package `plugin` is the generated package for |
| 61 | + `google/protobuf/compiler/plugin.proto`. |
| 62 | +* [`protoc-gen-go`](https://pkg.go.dev/github.com/golang/protobuf/protoc-gen-go): |
| 63 | + The `protoc-gen-go` binary is a protoc plugin to generate a Go protocol |
| 64 | + buffer package. |
| 65 | + |
| 66 | +## Reporting issues |
| 67 | + |
| 68 | +The issue tracker for this project |
| 69 | +[is located here](https://github.com/golang/protobuf/issues). |
| 70 | + |
| 71 | +Please report any issues with a sufficient description of the bug or feature |
| 72 | +request. Bug reports should ideally be accompanied by a minimal reproduction of |
| 73 | +the issue. Irreproducible bugs are difficult to diagnose and fix (and likely to |
| 74 | +be closed after some period of time). Bug reports must specify the version of |
| 75 | +the |
| 76 | +[Go protocol buffer module](https://github.com/protocolbuffers/protobuf-go/releases) |
| 77 | +and also the version of the |
| 78 | +[protocol buffer toolchain](https://github.com/protocolbuffers/protobuf/releases) |
| 79 | +being used. |
| 80 | + |
| 81 | +## Contributing |
| 82 | + |
| 83 | +This project is open-source and accepts contributions. See the |
| 84 | +[contribution guide](https://github.com/golang/protobuf/blob/master/CONTRIBUTING.md) |
| 85 | +for more information. |
| 86 | + |
| 87 | +## Compatibility |
| 88 | + |
| 89 | +This module and the generated code are expected to be stable over time. However, |
| 90 | +we reserve the right to make breaking changes without notice for the following |
| 91 | +reasons: |
| 92 | + |
| 93 | +* **Security:** A security issue in the specification or implementation may |
| 94 | + come to light whose resolution requires breaking compatibility. We reserve |
| 95 | + the right to address such issues. |
| 96 | +* **Unspecified behavior:** There are some aspects of the protocol buffer |
| 97 | + specification that are undefined. Programs that depend on unspecified |
| 98 | + behavior may break in future releases. |
| 99 | +* **Specification changes:** It may become necessary to address an |
| 100 | + inconsistency, incompleteness, or change in the protocol buffer |
| 101 | + specification, which may affect the behavior of existing programs. We |
| 102 | + reserve the right to address such changes. |
| 103 | +* **Bugs:** If a package has a bug that violates correctness, a program |
| 104 | + depending on the buggy behavior may break if the bug is fixed. We reserve |
| 105 | + the right to fix such bugs. |
| 106 | +* **Generated additions**: We reserve the right to add new declarations to |
| 107 | + generated Go packages of `.proto` files. This includes declared constants, |
| 108 | + variables, functions, types, fields in structs, and methods on types. This |
| 109 | + may break attempts at injecting additional code on top of what is generated |
| 110 | + by `protoc-gen-go`. Such practice is not supported by this project. |
| 111 | +* **Internal changes**: We reserve the right to add, modify, and remove |
| 112 | + internal code, which includes all unexported declarations, the |
| 113 | + [`generator`](https://pkg.go.dev/github.com/golang/protobuf/protoc-gen-go/generator) |
| 114 | + package, and all packages under |
| 115 | + [`internal`](https://pkg.go.dev/github.com/golang/protobuf/internal). |
264 | 116 |
|
265 | 117 | Any breaking changes outside of these will be announced 6 months in advance to
|
266 |
| - |
267 |
| - |
268 |
| -You should, whenever possible, use generated code created by the `protoc-gen-go` |
269 |
| -tool built at the same commit as the `proto` package. The `proto` package |
270 |
| -declares package-level constants in the form `ProtoPackageIsVersionX`. |
271 |
| -Application code and generated code may depend on one of these constants to |
272 |
| -ensure that compilation will fail if the available version of the proto library |
273 |
| -is too old. Whenever we make a change to the generated code that requires newer |
274 |
| -library support, in the same commit we will increment the version number of the |
275 |
| -generated code and declare a new package-level constant whose name incorporates |
276 |
| -the latest version number. Removing a compatibility constant is considered a |
277 |
| -breaking change and would be subject to the announcement policy stated above. |
278 |
| - |
279 |
| -The `protoc-gen-go/generator` package exposes a plugin interface, |
280 |
| -which is used by the gRPC code generation. This interface is not |
281 |
| -supported and is subject to incompatible changes without notice. |
| 118 | +[[email protected]](https://groups.google.com/forum/#!forum/protobuf). |
0 commit comments