Back to Go Zero

RPC Examples

tools/goctl/rpc/example/README.md

1.10.14.3 KB
Original Source

RPC Examples

This directory contains complete examples for all goctl rpc code generation scenarios.

Each example includes:

  • .proto source files
  • README.md (English) and README-cn.md (中文) documentation

Examples

#DirectoryScenarioKey Flags
0101-basicBasic single service, no imports
0202-import-siblingImport sibling proto file--proto_path=.
0303-import-subdirImport proto from subdirectory--proto_path=.
0404-transitive-importTransitive imports (A → B → C)--proto_path=.
0505-multiple-servicesMultiple services in one proto--multiple
0606-wellknown-typesGoogle well-known types in messages--proto_path=$PROTOC_INCLUDE
0707-external-proto-same-pkgExternal proto, same go_package-I ./ext_protos
0808-external-proto-diff-pkgExternal proto, different go_package-I ./ext_protos
0909-google-types-as-rpcGoogle types as RPC parameters--proto_path=$PROTOC_INCLUDE
1010-streamingServer/client/bidirectional streaming

Prerequisites

Quick Start

bash
# Install protoc plugins
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# Try the basic example
cd 01-basic
mkdir -p output && cd output && go mod init example.com/demo && cd ..
goctl rpc protoc greeter.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 .

RPC 示例

本目录包含所有 goctl rpc 代码生成场景的完整示例。

每个示例包含:

  • .proto 源文件
  • README.md(英文)和 README-cn.md(中文)文档

示例列表

#目录场景关键标志
0101-basic基础单服务,无导入
0202-import-sibling导入同级 proto 文件--proto_path=.
0303-import-subdir导入子目录中的 proto--proto_path=.
0404-transitive-import传递性导入(A → B → C)--proto_path=.
0505-multiple-services单 proto 多服务--multiple
0606-wellknown-types消息中使用 Google 标准类型--proto_path=$PROTOC_INCLUDE
0707-external-proto-same-pkg外部 proto,相同 go_package-I ./ext_protos
0808-external-proto-diff-pkg外部 proto,不同 go_package-I ./ext_protos
0909-google-types-as-rpcGoogle 类型作为 RPC 参数--proto_path=$PROTOC_INCLUDE
1010-streaming服务端/客户端/双向流

前置条件

快速开始

bash
# 安装 protoc 插件
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# 试试基础示例
cd 01-basic
mkdir -p output && cd output && go mod init example.com/demo && cd ..
goctl rpc protoc greeter.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 .