README 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. GoGoProtobuf http://github.com/gogo/protobuf extends
  2. GoProtobuf http://github.com/golang/protobuf
  3. # Go support for Protocol Buffers
  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.4.
  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 $GOBIN,
  24. defaulting to $GOPATH/bin. It must be in your $PATH for the protocol
  25. compiler, protoc, to find it.
  26. This software has two parts: a 'protocol compiler plugin' that
  27. generates Go source files that, once compiled, can access and manage
  28. protocol buffers; and a library that implements run-time support for
  29. encoding (marshaling), decoding (unmarshaling), and accessing protocol
  30. buffers.
  31. There is support for gRPC in Go using protocol buffers.
  32. See the note at the bottom of this file for details.
  33. There are no insertion points in the plugin.
  34. GoGoProtobuf provides extensions for protocol buffers and GoProtobuf
  35. see http://github.com/gogo/protobuf/gogoproto/doc.go
  36. ## Using protocol buffers with Go ##
  37. Once the software is installed, there are two steps to using it.
  38. First you must compile the protocol buffer definitions and then import
  39. them, with the support library, into your program.
  40. To compile the protocol buffer definition, run protoc with the --gogo_out
  41. parameter set to the directory you want to output the Go code to.
  42. protoc --gogo_out=. *.proto
  43. The generated files will be suffixed .pb.go. See the Test code below
  44. for an example using such a file.
  45. The package comment for the proto library contains text describing
  46. the interface provided in Go for protocol buffers. Here is an edited
  47. version.
  48. If you are using any gogo.proto extensions you will need to specify the
  49. proto_path to include the descriptor.proto and gogo.proto.
  50. gogo.proto is located in github.com/gogo/protobuf/gogoproto
  51. This should be fine, since your import is the same.
  52. descriptor.proto is located in either github.com/gogo/protobuf/protobuf
  53. or code.google.com/p/protobuf/trunk/src/
  54. Its import is google/protobuf/descriptor.proto so it might need some help.
  55. protoc --gogo_out=. -I=.:github.com/gogo/protobuf/protobuf *.proto
  56. ==========
  57. The proto package converts data structures to and from the
  58. wire format of protocol buffers. It works in concert with the
  59. Go source code generated for .proto files by the protocol compiler.
  60. A summary of the properties of the protocol buffer interface
  61. for a protocol buffer variable v:
  62. - Names are turned from camel_case to CamelCase for export.
  63. - There are no methods on v to set fields; just treat
  64. them as structure fields.
  65. - There are getters that return a field's value if set,
  66. and return the field's default value if unset.
  67. The getters work even if the receiver is a nil message.
  68. - The zero value for a struct is its correct initialization state.
  69. All desired fields must be set before marshaling.
  70. - A Reset() method will restore a protobuf struct to its zero state.
  71. - Non-repeated fields are pointers to the values; nil means unset.
  72. That is, optional or required field int32 f becomes F *int32.
  73. - Repeated fields are slices.
  74. - Helper functions are available to aid the setting of fields.
  75. Helpers for getting values are superseded by the
  76. GetFoo methods and their use is deprecated.
  77. msg.Foo = proto.String("hello") // set field
  78. - Constants are defined to hold the default values of all fields that
  79. have them. They have the form Default_StructName_FieldName.
  80. Because the getter methods handle defaulted values,
  81. direct use of these constants should be rare.
  82. - Enums are given type names and maps from names to values.
  83. Enum values are prefixed with the enum's type name. Enum types have
  84. a String method, and a Enum method to assist in message construction.
  85. - Nested groups and enums have type names prefixed with the name of
  86. the surrounding message type.
  87. - Extensions are given descriptor names that start with E_,
  88. followed by an underscore-delimited list of the nested messages
  89. that contain it (if any) followed by the CamelCased name of the
  90. extension field itself. HasExtension, ClearExtension, GetExtension
  91. and SetExtension are functions for manipulating extensions.
  92. - Oneof field sets are given a single field in their message,
  93. with distinguished wrapper types for each possible field value.
  94. - Marshal and Unmarshal are functions to encode and decode the wire format.
  95. When the .proto file specifies `syntax="proto3"`, there are some differences:
  96. - Non-repeated fields of non-message type are values instead of pointers.
  97. - Enum types do not get an Enum method.
  98. Consider file test.proto, containing
  99. ```proto
  100. package example;
  101. enum FOO { X = 17; };
  102. message Test {
  103. required string label = 1;
  104. optional int32 type = 2 [default=77];
  105. repeated int64 reps = 3;
  106. optional group OptionalGroup = 4 {
  107. required string RequiredField = 5;
  108. }
  109. }
  110. ```
  111. To create and play with a Test object from the example package,
  112. ```go
  113. package main
  114. import (
  115. "log"
  116. "github.com/gogo/protobuf/proto"
  117. "path/to/example"
  118. )
  119. func main() {
  120. test := &example.Test {
  121. Label: proto.String("hello"),
  122. Type: proto.Int32(17),
  123. Reps: []int64{1, 2, 3},
  124. Optionalgroup: &example.Test_OptionalGroup {
  125. RequiredField: proto.String("good bye"),
  126. },
  127. }
  128. data, err := proto.Marshal(test)
  129. if err != nil {
  130. log.Fatal("marshaling error: ", err)
  131. }
  132. newTest := &example.Test{}
  133. err = proto.Unmarshal(data, newTest)
  134. if err != nil {
  135. log.Fatal("unmarshaling error: ", err)
  136. }
  137. // Now test and newTest contain the same data.
  138. if test.GetLabel() != newTest.GetLabel() {
  139. log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
  140. }
  141. // etc.
  142. }
  143. ```
  144. ## Parameters ##
  145. To pass extra parameters to the plugin, use a comma-separated
  146. parameter list separated from the output directory by a colon:
  147. protoc --gogo_out=plugins=grpc,import_path=mypackage:. *.proto
  148. - `import_prefix=xxx` - a prefix that is added onto the beginning of
  149. all imports. Useful for things like generating protos in a
  150. subdirectory, or regenerating vendored protobufs in-place.
  151. - `import_path=foo/bar` - used as the package if no input files
  152. declare `go_package`. If it contains slashes, everything up to the
  153. rightmost slash is ignored.
  154. - `plugins=plugin1+plugin2` - specifies the list of sub-plugins to
  155. load. The only plugin in this repo is `grpc`.
  156. - `Mfoo/bar.proto=quux/shme` - declares that foo/bar.proto is
  157. associated with Go package quux/shme. This is subject to the
  158. import_prefix parameter.
  159. ## gRPC Support ##
  160. If a proto file specifies RPC services, protoc-gen-go can be instructed to
  161. generate code compatible with gRPC (http://www.grpc.io/). To do this, pass
  162. the `plugins` parameter to protoc-gen-go; the usual way is to insert it into
  163. the --go_out argument to protoc:
  164. protoc --gogo_out=plugins=grpc:. *.proto
  165. ## Compatibility ##
  166. The library and the generated code are expected to be stable over time.
  167. However, we reserve the right to make breaking changes without notice for the
  168. following reasons:
  169. - Security. A security issue in the specification or implementation may come to
  170. light whose resolution requires breaking compatibility. We reserve the right
  171. to address such security issues.
  172. - Unspecified behavior. There are some aspects of the Protocol Buffers
  173. specification that are undefined. Programs that depend on such unspecified
  174. behavior may break in future releases.
  175. - Specification errors or changes. If it becomes necessary to address an
  176. inconsistency, incompleteness, or change in the Protocol Buffers
  177. specification, resolving the issue could affect the meaning or legality of
  178. existing programs. We reserve the right to address such issues, including
  179. updating the implementations.
  180. - Bugs. If the library has a bug that violates the specification, a program
  181. that depends on the buggy behavior may break if the bug is fixed. We reserve
  182. the right to fix such bugs.
  183. - Adding methods or fields to generated structs. These may conflict with field
  184. names that already exist in a schema, causing applications to break. When the
  185. code generator encounters a field in the schema that would collide with a
  186. generated field or method name, the code generator will append an underscore
  187. to the generated field or method name.
  188. - Adding, removing, or changing methods or fields in generated structs that
  189. start with `XXX`. These parts of the generated code are exported out of
  190. necessity, but should not be considered part of the public API.
  191. - Adding, removing, or changing unexported symbols in generated code.
  192. Any breaking changes outside of these will be announced 6 months in advance to
  193. protobuf@googlegroups.com.
  194. You should, whenever possible, use generated code created by the `protoc-gen-go`
  195. tool built at the same commit as the `proto` package. The `proto` package
  196. declares package-level constants in the form `ProtoPackageIsVersionX`.
  197. Application code and generated code may depend on one of these constants to
  198. ensure that compilation will fail if the available version of the proto library
  199. is too old. Whenever we make a change to the generated code that requires newer
  200. library support, in the same commit we will increment the version number of the
  201. generated code and declare a new package-level constant whose name incorporates
  202. the latest version number. Removing a compatibility constant is considered a
  203. breaking change and would be subject to the announcement policy stated above.
  204. ## Plugins ##
  205. The `protoc-gen-go/generator` package exposes a plugin interface,
  206. which is used by the gRPC code generation. This interface is not
  207. supported and is subject to incompatible changes without notice.