README 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. Go support for Protocol Buffers - Google's data interchange format
  2. Copyright 2010 Google Inc.
  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 goinstall.
  26. # Grab the code from the repository and install the proto package.
  27. goinstall goprotobuf.googlecode.com/hg/proto
  28. # Compile and install the compiler plugin
  29. cd $GOROOT/src/pkg/goprotobuf.googlecode.com/hg/compiler
  30. make install
  31. The compiler plugin, protoc-gen-go, will be installed in $GOBIN,
  32. defaulting to $HOME/bin. It must be in your $PATH for the protocol
  33. compiler, protoc, to find it.
  34. Once the software is installed, there are two steps to using it.
  35. First you must compile the protocol buffer definitions and then import
  36. them, with the support library, into your program.
  37. To compile the protocol buffer definition, write a Makefile in the
  38. style shown in the comment in the file Make.protobuf. If your Makefile
  39. includes Make.protobuf, the rest should follow automatically. The
  40. generated code can be compiled separately or as part of a normal Go
  41. package.
  42. The generated files will be suffixed .pb.go. See the Test code below
  43. for an example using such a file.
  44. This repository uses the same code review mechanism as Go, so
  45. if you wish to submit changes add the equivalent of these two lines
  46. to $GOROOT/src/pkg/goprotobuf.googlecode.com/hg/.hg/hgrc
  47. [extensions]
  48. codereview = $GOROOT/lib/codereview/codereview.py
  49. *where $GOROOT is the expanded text, such as /usr/foo/go*.
  50. The package comment for the proto library contains text describing
  51. the interface provided in Go for protocol buffers. Here is an edited
  52. version.
  53. ==========
  54. The proto package converts data structures to and from the
  55. wire format of protocol buffers. It works in concert with the
  56. Go source code generated for .proto files by the protocol compiler.
  57. A summary of the properties of the protocol buffer interface
  58. for a protocol buffer variable v:
  59. - Names are turned from camel_case to CamelCase for export.
  60. - There are no methods on v to set and get fields; just treat
  61. them as structure fields.
  62. - The zero value for a struct is its correct initialization state.
  63. All desired fields must be set before marshaling.
  64. - A Reset() method will restore a protobuf struct to its zero state.
  65. - Non-repeated fields are pointers to the values; nil means unset.
  66. That is, optional or required field int32 f becomes F *int32.
  67. - Repeated fields are slices.
  68. - Helper functions are available to simplify the getting and setting of fields:
  69. foo.String = proto.String("hello") // set field
  70. s := proto.GetString(foo.String) // get 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. - Enums are given type names and maps between names to values,
  74. plus a helper function to create values. Enum values are prefixed
  75. with the enum's type name.
  76. - Nested groups and enums have type names prefixed with the name of
  77. the surrounding message type.
  78. - Marshal and Unmarshal are functions to encode and decode the wire format.
  79. Consider file test.proto, containing
  80. package example;
  81. enum FOO { X = 17; };
  82. message Test {
  83. required string label = 1;
  84. optional int32 type = 2 [default=77];
  85. repeated int64 reps = 3;
  86. optional group OptionalGroup = 4 {
  87. required string RequiredField = 5;
  88. };
  89. }
  90. To build a package from test.proto and some other Go files, write a
  91. Makefile like this:
  92. include $(GOROOT)/src/Make.$(GOARCH)
  93. TARG=path/to/example
  94. GOFILES=\
  95. test.pb.go\
  96. other.go
  97. include $(GOROOT)/src/Make.pkg
  98. include $(GOROOT)/src/pkg/goprotobuf.googlecode.com/hg/Make.protobuf
  99. To create and play with a Test object from the example package,
  100. package main
  101. import (
  102. "log"
  103. "goprotobuf.googlecode.com/hg/proto"
  104. "path/to/example"
  105. )
  106. func main() {
  107. test := &example.Test {
  108. Label: proto.String("hello"),
  109. Type: proto.Int32(17),
  110. Optionalgroup: &example.Test_OptionalGroup {
  111. RequiredField: proto.String("good bye"),
  112. },
  113. }
  114. data, err := proto.Marshal(test)
  115. if err != nil {
  116. log.Exit("marshaling error:", err)
  117. }
  118. newTest := &example.Test{}
  119. err = proto.Unmarshal(data, newTest)
  120. if err != nil {
  121. log.Exit("unmarshaling error:", err)
  122. }
  123. // Now test and newTest contain the same data.
  124. if proto.GetString(test.Label) != proto.GetString(newTest.Label) {
  125. log.Exit("data mismatch %q %q", proto.GetString(test.Label), proto.GetString(newTest.Label))
  126. }
  127. // etc.
  128. }