mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
Example 02: Importing a Sibling Proto File
This example demonstrates importing a proto file from the same directory.
Proto Definition
Two proto files in the same directory share the same go_package:
types.proto— Defines shared message types (User).user.proto— Defines the RPC service, importingtypes.proto.
Both files use the same go_package with a full module path:
option go_package = "example.com/demo/pb";
user.proto imports types.proto via:
import "types.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 user.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
│ └── usersvc.yaml
├── go.mod
├── internal
│ ├── config
│ │ └── config.go
│ ├── logic
│ │ ├── createuserlogic.go
│ │ └── getuserlogic.go
│ ├── server
│ │ └── userserviceserver.go
│ └── svc
│ └── servicecontext.go
├── pb
│ ├── types.pb.go
│ ├── user.pb.go
│ └── user_grpc.pb.go
├── userservice
│ └── userservice.go
└── usersvc.go
Key Points
- Two proto files (
user.protoandtypes.proto) share the samego_package = "example.com/demo/pb", compiled into a single Go package. user.protoimportstypes.protoviaimport "types.proto".- When multiple proto files share the same
go_package, they compile into a single Go package. - Only the proto file containing
servicedefinitions needs to be passed togoctl rpc protoc. - The imported proto is automatically compiled by protoc and resolved by goctl.