How do packages and imports work in Protocol Buffer definitions?
Last updated
A package declaration namespaces the types in a .proto file to avoid name collisions, and import pulls in definitions from another file so you can reference its types.
packagescoping: Types are referenced aspackage.TypeName; the package also influences generated code namespaces (unless overridden by options likejava_packageorgo_package).import: Takes a path relative to the proto include roots (the-I/proto_path), not a filesystem-relative path.import public: Re-exports the imported file's types so consumers of your file can use them transitively, useful when refactoring a file into pieces.No transitive visibility by default: A plain
importonly exposes that file's own types to you, not what it imports; import each file whose types you use directly.
[lang:protobuf]
syntax = "proto3";
package myapp.orders;
import "myapp/common/money.proto";
message Order {
myapp.common.Money total = 1;
}