commit | 013f295b1c740bd8ca5ce84ea810940b1945fcb0 | [log] [tgz] |
---|---|---|
author | Tamir Duberstein <tamird@gmail.com> | Mon Nov 20 05:39:45 2017 -0500 |
committer | Joe Tsai <joetsai@digital-static.net> | Mon Nov 20 18:39:45 2017 +0800 |
tree | b47005dd6b5794ec33f8f0cc2b6bedddec2dc1d3 | |
parent | 3ffccb49d84ed0b9eb1e518dd391a6d015adfeb4 [diff] |
Correct some mistakes (#316) * Regenerate * Correct some mistakes Most notable is the bug in (*proto.Properties).Parse, for which I've added a test. Found with honnef.co/go/tools/cmd/staticcheck. proto/properties.go:218:2: this value of s is never used (SA4006) proto/properties.go:329:5: ineffective break statement. Did you mean to break out of the outer loop? (SA4011) protoc-gen-go/generator/generator.go:1686:5: this value of obj is never used (SA4006)
Google's data interchange format. Copyright 2010 The Go Authors. https://github.com/golang/protobuf
This package and the code it generates requires at least Go 1.4.
This software implements Go bindings for protocol buffers. For information about protocol buffers themselves, see https://developers.google.com/protocol-buffers/
To use this software, you must:
go get -u github.com/golang/protobuf/protoc-gen-go
. The compiler plugin, protoc-gen-go, will be installed in $GOBIN, defaulting to $GOPATH/bin. It must be in your $PATH for the protocol compiler, protoc, to find it.This software has two parts: a 'protocol compiler plugin' that generates Go source files that, once compiled, can access and manage protocol buffers; and a library that implements run-time support for encoding (marshaling), decoding (unmarshaling), and accessing protocol buffers.
There is support for gRPC in Go using protocol buffers. See the note at the bottom of this file for details.
There are no insertion points in the plugin.
Once the software is installed, there are two steps to using it. First you must compile the protocol buffer definitions and then import them, with the support library, into your program.
To compile the protocol buffer definition, run protoc with the --go_out parameter set to the directory you want to output the Go code to.
protoc --go_out=. *.proto
The generated files will be suffixed .pb.go. See the Test code below for an example using such a file.
The package comment for the proto library contains text describing the interface provided in Go for protocol buffers. Here is an edited version.
==========
The proto package converts data structures to and from the wire format of protocol buffers. It works in concert with the Go source code generated for .proto files by the protocol compiler.
A summary of the properties of the protocol buffer interface for a protocol buffer variable v:
When the .proto file specifies syntax="proto3"
, there are some differences:
Consider file test.proto, containing
syntax = "proto2"; package example; enum FOO { X = 17; }; message Test { required string label = 1; optional int32 type = 2 [default=77]; repeated int64 reps = 3; optional group OptionalGroup = 4 { required string RequiredField = 5; } }
To create and play with a Test object from the example package,
package main import ( "log" "github.com/golang/protobuf/proto" "path/to/example" ) func main() { test := &example.Test { Label: proto.String("hello"), Type: proto.Int32(17), Reps: []int64{1, 2, 3}, Optionalgroup: &example.Test_OptionalGroup { RequiredField: proto.String("good bye"), }, } data, err := proto.Marshal(test) if err != nil { log.Fatal("marshaling error: ", err) } newTest := &example.Test{} err = proto.Unmarshal(data, newTest) if err != nil { log.Fatal("unmarshaling error: ", err) } // Now test and newTest contain the same data. if test.GetLabel() != newTest.GetLabel() { log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel()) } // etc. }
To pass extra parameters to the plugin, use a comma-separated parameter list separated from the output directory by a colon:
protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto
import_prefix=xxx
- a prefix that is added onto the beginning of all imports. Useful for things like generating protos in a subdirectory, or regenerating vendored protobufs in-place.import_path=foo/bar
- used as the package if no input files declare go_package
. If it contains slashes, everything up to the rightmost slash is ignored.plugins=plugin1+plugin2
- specifies the list of sub-plugins to load. The only plugin in this repo is grpc
.Mfoo/bar.proto=quux/shme
- declares that foo/bar.proto is associated with Go package quux/shme. This is subject to the import_prefix parameter.If a proto file specifies RPC services, protoc-gen-go can be instructed to generate code compatible with gRPC (http://www.grpc.io/). To do this, pass the plugins
parameter to protoc-gen-go; the usual way is to insert it into the --go_out argument to protoc:
protoc --go_out=plugins=grpc:. *.proto
The library and the generated code are expected to be stable over time. However, we reserve the right to make breaking changes without notice for the following reasons:
XXX
. These parts of the generated code are exported out of necessity, but should not be considered part of the public API.Any breaking changes outside of these will be announced 6 months in advance to protobuf@googlegroups.com.
You should, whenever possible, use generated code created by the protoc-gen-go
tool built at the same commit as the proto
package. The proto
package declares package-level constants in the form ProtoPackageIsVersionX
. Application code and generated code may depend on one of these constants to ensure that compilation will fail if the available version of the proto library is too old. Whenever we make a change to the generated code that requires newer library support, in the same commit we will increment the version number of the generated code and declare a new package-level constant whose name incorporates the latest version number. Removing a compatibility constant is considered a breaking change and would be subject to the announcement policy stated above.
The protoc-gen-go/generator
package exposes a plugin interface, which is used by the gRPC code generation. This interface is not supported and is subject to incompatible changes without notice.