Files
go-zero/tools/goctl/rpc/example/04-transitive-import

Example 04: Transitive Imports

This example demonstrates transitive proto imports, where A imports B and B imports C.

Proto Definition

Three proto files form a transitive import chain, all sharing the same go_package:

option go_package = "example.com/demo/pb";
  • base.proto — Layer C: defines base types (BaseResp).
  • middleware.proto — Layer B: imports base.proto, defines RequestMeta.
  • main.proto — Layer A: imports middleware.proto, defines the PingService (entry point).

Import chain: main.protomiddleware.protobase.proto

Generation Commands

First, initialize the output directory with a go.mod:

mkdir -p output && cd output && go mod init example.com/demo && cd ..

Then generate the code:

goctl rpc protoc main.proto \
  --go_out=output \
  --go-grpc_out=output \
  --zrpc_out=output \
  --go_opt=module=example.com/demo \
  --go-grpc_opt=module=example.com/demo \
  --module=example.com/demo \
  -I .

Generated directory structure:

output/
├── etc
│   └── pingsvc.yaml
├── go.mod
├── internal
│   ├── config
│   │   └── config.go
│   ├── logic
│   │   └── pinglogic.go
│   ├── server
│   │   └── pingserviceserver.go
│   └── svc
│       └── servicecontext.go
├── pb
│   ├── base.pb.go
│   ├── main.pb.go
│   ├── main_grpc.pb.go
│   └── middleware.pb.go
├── pingservice
│   └── pingservice.go
└── pingsvc.go

Key Points

  • Three proto files (base.protomiddleware.protomain.proto) form a transitive import chain.
  • goctl recursively resolves all transitive imports automatically.
  • All three files share the same go_package = "example.com/demo/pb".
  • You only need to specify the entry proto file — goctl and protoc handle the rest.
  • Circular imports are detected and will cause an error (same as protoc behavior).