You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # Go support for Protocol Buffers - Google's data interchange format
  2. [![Build Status](https://travis-ci.org/golang/protobuf.svg?branch=master)](https://travis-ci.org/golang/protobuf)
  3. [![GoDoc](https://godoc.org/github.com/golang/protobuf?status.svg)](https://godoc.org/github.com/golang/protobuf)
  4. Google's data interchange format.
  5. Copyright 2010 The Go Authors.
  6. https://github.com/golang/protobuf
  7. This package and the code it generates requires at least Go 1.9.
  8. This software implements Go bindings for protocol buffers. For
  9. information about protocol buffers themselves, see
  10. https://developers.google.com/protocol-buffers/
  11. ## Installation ##
  12. To use this software, you must:
  13. - Install the standard C++ implementation of protocol buffers from
  14. https://developers.google.com/protocol-buffers/
  15. - Of course, install the Go compiler and tools from
  16. https://golang.org/
  17. See
  18. https://golang.org/doc/install
  19. for details or, if you are using gccgo, follow the instructions at
  20. https://golang.org/doc/install/gccgo
  21. - Grab the code from the repository and install the `proto` package.
  22. The simplest way is to run `go get -u github.com/golang/protobuf/protoc-gen-go`.
  23. The compiler plugin, `protoc-gen-go`, will be installed in `$GOPATH/bin`
  24. unless `$GOBIN` is set. It must be in your `$PATH` for the protocol
  25. compiler, `protoc`, to find it.
  26. - If you need a particular version of `protoc-gen-go` (e.g., to match your
  27. `proto` package version), one option is
  28. ```shell
  29. GIT_TAG="v1.2.0" # change as needed
  30. go get -d -u github.com/golang/protobuf/protoc-gen-go
  31. git -C "$(go env GOPATH)"/src/github.com/golang/protobuf checkout $GIT_TAG
  32. go install github.com/golang/protobuf/protoc-gen-go
  33. ```
  34. This software has two parts: a 'protocol compiler plugin' that
  35. generates Go source files that, once compiled, can access and manage
  36. protocol buffers; and a library that implements run-time support for
  37. encoding (marshaling), decoding (unmarshaling), and accessing protocol
  38. buffers.
  39. There is support for gRPC in Go using protocol buffers.
  40. See the note at the bottom of this file for details.
  41. There are no insertion points in the plugin.
  42. ## Using protocol buffers with Go ##
  43. Once the software is installed, there are two steps to using it.
  44. First you must compile the protocol buffer definitions and then import
  45. them, with the support library, into your program.
  46. To compile the protocol buffer definition, run protoc with the --go_out
  47. parameter set to the directory you want to output the Go code to.
  48. protoc --go_out=. *.proto
  49. The generated files will be suffixed .pb.go. See the Test code below
  50. for an example using such a file.
  51. ## Packages and input paths ##
  52. The protocol buffer language has a concept of "packages" which does not
  53. correspond well to the Go notion of packages. In generated Go code,
  54. each source `.proto` file is associated with a single Go package. The
  55. name and import path for this package is specified with the `go_package`
  56. proto option:
  57. option go_package = "github.com/golang/protobuf/ptypes/any";
  58. The protocol buffer compiler will attempt to derive a package name and
  59. import path if a `go_package` option is not present, but it is
  60. best to always specify one explicitly.
  61. There is a one-to-one relationship between source `.proto` files and
  62. generated `.pb.go` files, but any number of `.pb.go` files may be
  63. contained in the same Go package.
  64. The output name of a generated file is produced by replacing the
  65. `.proto` suffix with `.pb.go` (e.g., `foo.proto` produces `foo.pb.go`).
  66. However, the output directory is selected in one of two ways. Let
  67. us say we have `inputs/x.proto` with a `go_package` option of
  68. `github.com/golang/protobuf/p`. The corresponding output file may
  69. be:
  70. - Relative to the import path:
  71. ```shell
  72. protoc --go_out=. inputs/x.proto
  73. # writes ./github.com/golang/protobuf/p/x.pb.go
  74. ```
  75. (This can work well with `--go_out=$GOPATH`.)
  76. - Relative to the input file:
  77. ```shell
  78. protoc --go_out=paths=source_relative:. inputs/x.proto
  79. # generate ./inputs/x.pb.go
  80. ```
  81. ## Generated code ##
  82. The package comment for the proto library contains text describing
  83. the interface provided in Go for protocol buffers. Here is an edited
  84. version.
  85. The proto package converts data structures to and from the
  86. wire format of protocol buffers. It works in concert with the
  87. Go source code generated for .proto files by the protocol compiler.
  88. A summary of the properties of the protocol buffer interface
  89. for a protocol buffer variable v:
  90. - Names are turned from camel_case to CamelCase for export.
  91. - There are no methods on v to set fields; just treat
  92. them as structure fields.
  93. - There are getters that return a field's value if set,
  94. and return the field's default value if unset.
  95. The getters work even if the receiver is a nil message.
  96. - The zero value for a struct is its correct initialization state.
  97. All desired fields must be set before marshaling.
  98. - A Reset() method will restore a protobuf struct to its zero state.
  99. - Non-repeated fields are pointers to the values; nil means unset.
  100. That is, optional or required field int32 f becomes F *int32.
  101. - Repeated fields are slices.
  102. - Helper functions are available to aid the setting of fields.
  103. Helpers for getting values are superseded by the
  104. GetFoo methods and their use is deprecated.
  105. msg.Foo = proto.String("hello") // set field
  106. - Constants are defined to hold the default values of all fields that
  107. have them. They have the form Default_StructName_FieldName.
  108. Because the getter methods handle defaulted values,
  109. direct use of these constants should be rare.
  110. - Enums are given type names and maps from names to values.
  111. Enum values are prefixed with the enum's type name. Enum types have
  112. a String method, and a Enum method to assist in message construction.
  113. - Nested groups and enums have type names prefixed with the name of
  114. the surrounding message type.
  115. - Extensions are given descriptor names that start with E_,
  116. followed by an underscore-delimited list of the nested messages
  117. that contain it (if any) followed by the CamelCased name of the
  118. extension field itself. HasExtension, ClearExtension, GetExtension
  119. and SetExtension are functions for manipulating extensions.
  120. - Oneof field sets are given a single field in their message,
  121. with distinguished wrapper types for each possible field value.
  122. - Marshal and Unmarshal are functions to encode and decode the wire format.
  123. When the .proto file specifies `syntax="proto3"`, there are some differences:
  124. - Non-repeated fields of non-message type are values instead of pointers.
  125. - Enum types do not get an Enum method.
  126. Consider file test.proto, containing
  127. ```proto
  128. syntax = "proto2";
  129. package example;
  130. enum FOO { X = 17; };
  131. message Test {
  132. required string label = 1;
  133. optional int32 type = 2 [default=77];
  134. repeated int64 reps = 3;
  135. }
  136. ```
  137. To create and play with a Test object from the example package,
  138. ```go
  139. package main
  140. import (
  141. "log"
  142. "github.com/golang/protobuf/proto"
  143. "path/to/example"
  144. )
  145. func main() {
  146. test := &example.Test{
  147. Label: proto.String("hello"),
  148. Type: proto.Int32(17),
  149. Reps: []int64{1, 2, 3},
  150. }
  151. data, err := proto.Marshal(test)
  152. if err != nil {
  153. log.Fatal("marshaling error: ", err)
  154. }
  155. newTest := &example.Test{}
  156. err = proto.Unmarshal(data, newTest)
  157. if err != nil {
  158. log.Fatal("unmarshaling error: ", err)
  159. }
  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())
  163. }
  164. // etc.
  165. }
  166. ```
  167. ## Parameters ##
  168. To pass extra parameters to the plugin, use a comma-separated
  169. parameter list separated from the output directory by a colon:
  170. protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto
  171. - `paths=(import | source_relative)` - specifies how the paths of
  172. generated files are structured. See the "Packages and imports paths"
  173. section above. The default is `import`.
  174. - `plugins=plugin1+plugin2` - specifies the list of sub-plugins to
  175. load. The only plugin in this repo is `grpc`.
  176. - `Mfoo/bar.proto=quux/shme` - declares that foo/bar.proto is
  177. associated with Go package quux/shme. This is subject to the
  178. import_prefix parameter.
  179. The following parameters are deprecated and should not be used:
  180. - `import_prefix=xxx` - a prefix that is added onto the beginning of
  181. all imports.
  182. - `import_path=foo/bar` - used as the package if no input files
  183. declare `go_package`. If it contains slashes, everything up to the
  184. rightmost slash is ignored.
  185. ## gRPC Support ##
  186. If a proto file specifies RPC services, protoc-gen-go can be instructed to
  187. generate code compatible with gRPC (http://www.grpc.io/). To do this, pass
  188. the `plugins` parameter to protoc-gen-go; the usual way is to insert it into
  189. the --go_out argument to protoc:
  190. protoc --go_out=plugins=grpc:. *.proto
  191. ## Compatibility ##
  192. The library and the generated code are expected to be stable over time.
  193. However, we reserve the right to make breaking changes without notice for the
  194. following reasons:
  195. - Security. A security issue in the specification or implementation may come to
  196. light whose resolution requires breaking compatibility. We reserve the right
  197. to address such security issues.
  198. - Unspecified behavior. There are some aspects of the Protocol Buffers
  199. specification that are undefined. Programs that depend on such unspecified
  200. behavior may break in future releases.
  201. - Specification errors or changes. If it becomes necessary to address an
  202. inconsistency, incompleteness, or change in the Protocol Buffers
  203. specification, resolving the issue could affect the meaning or legality of
  204. existing programs. We reserve the right to address such issues, including
  205. updating the implementations.
  206. - Bugs. If the library has a bug that violates the specification, a program
  207. that depends on the buggy behavior may break if the bug is fixed. We reserve
  208. the right to fix such bugs.
  209. - Adding methods or fields to generated structs. These may conflict with field
  210. names that already exist in a schema, causing applications to break. When the
  211. code generator encounters a field in the schema that would collide with a
  212. generated field or method name, the code generator will append an underscore
  213. to the generated field or method name.
  214. - Adding, removing, or changing methods or fields in generated structs that
  215. start with `XXX`. These parts of the generated code are exported out of
  216. necessity, but should not be considered part of the public API.
  217. - Adding, removing, or changing unexported symbols in generated code.
  218. Any breaking changes outside of these will be announced 6 months in advance to
  219. protobuf@googlegroups.com.
  220. You should, whenever possible, use generated code created by the `protoc-gen-go`
  221. tool built at the same commit as the `proto` package. The `proto` package
  222. declares package-level constants in the form `ProtoPackageIsVersionX`.
  223. Application code and generated code may depend on one of these constants to
  224. ensure that compilation will fail if the available version of the proto library
  225. is too old. Whenever we make a change to the generated code that requires newer
  226. library support, in the same commit we will increment the version number of the
  227. generated code and declare a new package-level constant whose name incorporates
  228. the latest version number. Removing a compatibility constant is considered a
  229. breaking change and would be subject to the announcement policy stated above.
  230. The `protoc-gen-go/generator` package exposes a plugin interface,
  231. which is used by the gRPC code generation. This interface is not
  232. supported and is subject to incompatible changes without notice.