Proto3: Packages - 包

You can add an optional package specifier to a .proto file to prevent name clashes between protocol message types.
你可以在.proto文件中添加一个可选package标识符,以防止protocol message类型命名冲突。

package foo.bar;
message Open { ... }

You can then use the package specifier when defining fields of your message type:
之后你就可以在message类型中定义字段时使用该package标识符:

message Foo {
  ...
  foo.bar.Open open = 1;
  ...
}

The way a package specifier affects the generated code depends on your chosen language:
包标识符如何影响生成的代码取决于你所选语言:

  • In C++ the generated classes are wrapped inside a C++ namespace. For example, Open would be in the namespace foo::bar.
    在C++中,生成的类被包裹在一个C++命名空间中。举个例子,Open类会在命名空间foo::bar下。

  • In Java and Kotlin, the package is used as the Java package, unless you explicitly provide an option java_package in your .proto file.
    在Java和Kotlin中,package会被用作Java package,除非你在你的.proto文件中显示的提供一个java_package选项

  • In Python, the package directive is ignored, since Python modules are organized according to their location in the file system.
    在Python中,package指令会被忽略,因为Python模块是根据它们在文件系统中的位置规划的。

  • In Go, the package is used as the Go package name, unless you explicitly provide an option go_package in your .proto file.
    在Go中,package被用作Go package名,除非你在你的.proto文件中显示地提供一个go_package选项。

  • In Ruby, the generated classes are wrapped inside nested Ruby namespaces, converted to the required Ruby capitalization style (first letter capitalized; if the first character is not a letter, PB_ is prepended). For example, Open would be in the namespace Foo::Bar.
    在Ruby中,生成的类是被包装在内嵌Ruby命名空间中,被转化为所要求的Ruby大写风格(首字母大写;如果首字符不是字母,则在首部追加PB_)。举个例子,Open类会在命名空间Foo::Bar下。

  • In C# the package is used as the namespace after converting to PascalCase, unless you explicitly provide an option csharp_namespace in your .proto file. For example, Open would be in the namespace Foo.Bar.
    在C#中,package转化为帕斯卡命名(大驼峰命名)后被用作命名空间,除非你在你的.proto文件中显示地提供一个csharp_namespace选项。举个例子,Open类会在命名空间Foo.Bar下。

Packages and Name Resolution - 包与名称解析

Type name resolution in the protocol buffer language works like C++: first the innermost scope is searched, then the next-innermost, and so on, with each package considered to be "inner" to its parent package. A leading '.' (for example, .foo.bar.Baz) means to start from the outermost scope instead.
在protocol buffer语言中类型名称解析类似C++:首先最内层作用域被搜索到,然后下一个最内层等等。每一个包被认为是其父包的“内部”。一个领先'.'(举个例子,.foo.bar.Baz)表示从最外层的作用域开始。

The protocol buffer compiler resolves all type names by parsing the imported .proto files. The code generator for each language knows how to refer to each type in that language, even if it has different scoping rules.
protocol buffer编译器通过解析导入的.proto文件解析所有类型名称。每种语言代码生成器知道如何引用该语言中的每个类型。尽管它有不同的作用域规则。

你可能感兴趣的:(Proto3: Packages - 包)