README 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. Go support for Protocol Buffers - Google's data interchange format
  2. Copyright 2010 The Go Authors.
  3. http://code.google.com/p/goprotobuf/
  4. This package and the code it generates requires at least Go 1.1.
  5. This software implements Go bindings for protocol buffers. For
  6. information about protocol buffers themselves, see
  7. http://code.google.com/apis/protocolbuffers/
  8. To use this software, you must first install the standard C++
  9. implementation of protocol buffers from
  10. http://code.google.com/p/protobuf/
  11. And of course you must also install the Go compiler and tools from
  12. http://code.google.com/p/go/
  13. See
  14. http://golang.org/doc/install.html
  15. for details or, if you are using gccgo, follow the instructions at
  16. http://golang.org/doc/gccgo_install.html
  17. This software has two parts: a 'protocol compiler plugin' that
  18. generates Go source files that, once compiled, can access and manage
  19. protocol buffers; and a library that implements run-time support for
  20. encoding (marshaling), decoding (unmarshaling), and accessing protocol
  21. buffers.
  22. There is no support for RPC in Go using protocol buffers. It may come
  23. once a standard RPC protocol develops for protobufs.
  24. There are no insertion points in the plugin.
  25. To install this code:
  26. The simplest way is to run go get.
  27. # Grab the code from the repository and install the proto package.
  28. go get -u code.google.com/p/goprotobuf/{proto,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. Once the software is installed, there are two steps to using it.
  33. First you must compile the protocol buffer definitions and then import
  34. them, with the support library, into your program.
  35. To compile the protocol buffer definition, run protoc with the --go_out
  36. parameter set to the directory you want to output the Go code to.
  37. protoc --go_out=. *.proto
  38. The generated files will be suffixed .pb.go. See the Test code below
  39. for an example using such a file.
  40. This repository uses the same code review mechanism as Go, so
  41. if you wish to submit changes add the equivalent of these two lines
  42. to $GOROOT/src/pkg/code.google.com/p/goprotobuf/.hg/hgrc
  43. [extensions]
  44. codereview = $GOROOT/lib/codereview/codereview.py
  45. *where $GOROOT is the expanded text, such as /usr/foo/go*.
  46. The package comment for the proto library contains text describing
  47. the interface provided in Go for protocol buffers. Here is an edited
  48. version.
  49. ==========
  50. The proto package converts data structures to and from the
  51. wire format of protocol buffers. It works in concert with the
  52. Go source code generated for .proto files by the protocol compiler.
  53. A summary of the properties of the protocol buffer interface
  54. for a protocol buffer variable v:
  55. - Names are turned from camel_case to CamelCase for export.
  56. - There are no methods on v to set fields; just treat
  57. them as structure fields.
  58. - There are getters that return a field's value if set,
  59. and return the field's default value if unset.
  60. The getters work even if the receiver is a nil message.
  61. - The zero value for a struct is its correct initialization state.
  62. All desired fields must be set before marshaling.
  63. - A Reset() method will restore a protobuf struct to its zero state.
  64. - Non-repeated fields are pointers to the values; nil means unset.
  65. That is, optional or required field int32 f becomes F *int32.
  66. - Repeated fields are slices.
  67. - Helper functions are available to aid the setting of fields.
  68. Helpers for getting values are superseded by the
  69. GetFoo methods and their use is deprecated.
  70. msg.Foo = proto.String("hello") // set field
  71. - Constants are defined to hold the default values of all fields that
  72. have them. They have the form Default_StructName_FieldName.
  73. Because the getter methods handle defaulted values,
  74. direct use of these constants should be rare.
  75. - Enums are given type names and maps from names to values.
  76. Enum values are prefixed with the enum's type name. Enum types have
  77. a String method, and a Enum method to assist in message construction.
  78. - Nested groups and enums have type names prefixed with the name of
  79. the surrounding message type.
  80. - Extensions are given descriptor names that start with E_,
  81. followed by an underscore-delimited list of the nested messages
  82. that contain it (if any) followed by the CamelCased name of the
  83. extension field itself. HasExtension, ClearExtension, GetExtension
  84. and SetExtension are functions for manipulating extensions.
  85. - Marshal and Unmarshal are functions to encode and decode the wire format.
  86. Consider file test.proto, containing
  87. package example;
  88. enum FOO { X = 17; };
  89. message Test {
  90. required string label = 1;
  91. optional int32 type = 2 [default=77];
  92. repeated int64 reps = 3;
  93. optional group OptionalGroup = 4 {
  94. required string RequiredField = 5;
  95. }
  96. }
  97. To build a package from test.proto and some other Go files, write a
  98. Makefile like this:
  99. include $(GOROOT)/src/Make.$(GOARCH)
  100. TARG=path/to/example
  101. GOFILES=\
  102. test.pb.go\
  103. other.go
  104. include $(GOROOT)/src/Make.pkg
  105. include $(GOROOT)/src/pkg/code.google.com/p/goprotobuf/Make.protobuf
  106. To create and play with a Test object from the example package,
  107. package main
  108. import (
  109. "log"
  110. "code.google.com/p/goprotobuf/proto"
  111. "path/to/example"
  112. )
  113. func main() {
  114. test := &example.Test {
  115. Label: proto.String("hello"),
  116. Type: proto.Int32(17),
  117. Optionalgroup: &example.Test_OptionalGroup {
  118. RequiredField: proto.String("good bye"),
  119. },
  120. }
  121. data, err := proto.Marshal(test)
  122. if err != nil {
  123. log.Fatal("marshaling error: ", err)
  124. }
  125. newTest := &example.Test{}
  126. err = proto.Unmarshal(data, newTest)
  127. if err != nil {
  128. log.Fatal("unmarshaling error: ", err)
  129. }
  130. // Now test and newTest contain the same data.
  131. if test.GetLabel() != newTest.GetLabel() {
  132. log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
  133. }
  134. // etc.
  135. }