mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
2.1 KiB
2.1 KiB
Example 07: External Proto — Same go_package
This example demonstrates importing proto files from an external directory where both files share the same go_package.
Proto Definition
Both service.proto and ext.proto use the same go_package:
option go_package = "example.com/demo/pb";
Source layout:
07-external-proto-same-pkg/
├── ext_protos
│ └── ext.proto # External proto (go_package = "example.com/demo/pb")
├── service.proto # Service definition (go_package = "example.com/demo/pb")
├── README.md
└── README-cn.md
ext.protolives in a separate directory (ext_protos/), but has the samego_packageasservice.proto.service.protoimportsext.protoand usesext.ExtReq/ext.ExtReplyas RPC types.
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 (note -I ./ext_protos):
goctl rpc protoc service.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 . -I ./ext_protos
Generated directory structure:
output/
├── etc
│ └── svc.yaml
├── go.mod
├── internal
│ ├── config
│ │ └── config.go
│ ├── logic
│ │ └── querylogic.go
│ ├── server
│ │ └── queryserviceserver.go
│ └── svc
│ └── servicecontext.go
├── pb
│ ├── ext.pb.go
│ ├── service.pb.go
│ └── service_grpc.pb.go
├── queryservice
│ └── queryservice.go
└── svc.go
Key Points
ext.protolives in a separate directory (ext_protos/), but has the samego_packageasservice.proto.- Use
-I ./ext_protosto add the external directory to the proto search path. - When the external proto has the same
go_package, all types merge into one Go package — no cross-package imports are needed.