README 5.7 KB

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