@@ -7,7 +7,7 @@ Google's data interchange format.
77Copyright 2010 The Go Authors.
88https://github.com/golang/protobuf
99
10- This package and the code it generates requires at least Go 1.4 .
10+ This package and the code it generates requires at least Go 1.6 .
1111
1212This software implements Go bindings for protocol buffers. For
1313information about protocol buffers themselves, see
@@ -56,13 +56,49 @@ parameter set to the directory you want to output the Go code to.
5656The generated files will be suffixed .pb.go. See the Test code below
5757for an example using such a file.
5858
59+ ## Packages and input paths ##
60+
61+ The protocol buffer language has a concept of "packages" which does not
62+ correspond well to the Go notion of packages. In generated Go code,
63+ each source ` .proto ` file is associated with a single Go package. The
64+ name and import path for this package is specified with the ` go_package `
65+ proto option:
66+
67+ option go_package = "github.com/golang/protobuf/ptypes/any";
68+
69+ The protocol buffer compiler will attempt to derive a package name and
70+ import path if a ` go_package ` option is not present, but it is
71+ best to always specify one explicitly.
72+
73+ There is a one-to-one relationship between source ` .proto ` files and
74+ generated ` .pb.go ` files, but any number of ` .pb.go ` files may be
75+ contained in the same Go package.
76+
77+ The output name of a generated file is produced by replacing the
78+ ` .proto ` suffix with ` .pb.go ` (e.g., ` foo.proto ` produces ` foo.pb.go ` ).
79+ However, the output directory is selected in one of two ways. Let
80+ us say we have ` inputs/x.proto ` with a ` go_package ` option of
81+ ` github.com/golang/protobuf/p ` . The corresponding output file may
82+ be:
83+
84+ - Relative to the import path:
85+
86+ protoc --go_out=. inputs/x.proto
87+ # writes ./github.com/golang/protobuf/p/x.pb.go
88+
89+ (This can work well with ` --go_out=$GOPATH ` .)
90+
91+ - Relative to the input file:
92+
93+ protoc --go_out=paths=source_relative:. inputs/x.proto
94+ # generate ./inputs/x.pb.go
95+
96+ ## Generated code ##
5997
6098The package comment for the proto library contains text describing
6199the interface provided in Go for protocol buffers. Here is an edited
62100version.
63101
64- ==========
65-
66102The proto package converts data structures to and from the
67103wire format of protocol buffers. It works in concert with the
68104Go source code generated for .proto files by the protocol compiler.
@@ -114,9 +150,9 @@ Consider file test.proto, containing
114150``` proto
115151 syntax = "proto2";
116152 package example;
117-
153+
118154 enum FOO { X = 17; };
119-
155+
120156 message Test {
121157 required string label = 1;
122158 optional int32 type = 2 [default=77];
@@ -170,22 +206,25 @@ To create and play with a Test object from the example package,
170206To pass extra parameters to the plugin, use a comma-separated
171207parameter list separated from the output directory by a colon:
172208
173-
174209 protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto
175210
176-
177- - ` import_prefix=xxx ` - a prefix that is added onto the beginning of
178- all imports. Useful for things like generating protos in a
179- subdirectory, or regenerating vendored protobufs in-place.
180- - ` import_path=foo/bar ` - used as the package if no input files
181- declare ` go_package ` . If it contains slashes, everything up to the
182- rightmost slash is ignored.
211+ - ` paths=(import | source_relative) ` - specifies how the paths of
212+ generated files are structured. See the "Packages and imports paths"
213+ section above. The default is ` import ` .
183214- ` plugins=plugin1+plugin2 ` - specifies the list of sub-plugins to
184215 load. The only plugin in this repo is ` grpc ` .
185216- ` Mfoo/bar.proto=quux/shme ` - declares that foo/bar.proto is
186217 associated with Go package quux/shme. This is subject to the
187218 import_prefix parameter.
188219
220+ The following parameters are deprecated and should not be used:
221+
222+ - ` import_prefix=xxx ` - a prefix that is added onto the beginning of
223+ all imports.
224+ - ` import_path=foo/bar ` - used as the package if no input files
225+ declare ` go_package ` . If it contains slashes, everything up to the
226+ rightmost slash is ignored.
227+
189228## gRPC Support ##
190229
191230If a proto file specifies RPC services, protoc-gen-go can be instructed to
0 commit comments