A protoc plugin that generates ConnectRPC handler skeletons from .proto definitions without ever overwriting developer code.
- Zero-clobbering guarantee - never overwrites your implementation code
- Two generation modes: per-service (default) or per-method file organization
- Smart regeneration - only adds new method stubs for new RPCs
- Flexible output directories with placeholder patterns
- Compile-time safety via interface checks
go install github.com/jackchuka/protoc-gen-connect-go-handler/cmd/protoc-gen-connect-go-handler@latest
plugins: - local: protoc-gen-connect-go-handler out: gen/go opt: out=gen/go
plugins: - local: protoc-gen-connect-go-handler out: service opt: out=service,mode=per_method,dir_pattern={package_path}/{service_snake},impl_suffix=_impl
This creates a structure like:
internal/handlers/
└── test/v1/test_service/
├── test_service_impl.gen.go # manifest (always regenerated)
├── test_service_impl.go # struct (created once)
└── test_service_echo.go # per-method files
| Purpose | File Pattern | Overwritten? | Editable? |
|---|---|---|---|
| Manifest (interface checks) | *{impl_suffix}.gen.go |
Always | ❌ |
| Struct (add fields here) | *{impl_suffix}.go |
First run only | ✅ |
| Method stubs | Same as struct (per*service) or \**{method}.go (per_method) |
New RPCs only | ✅ |
| Flag | Default | Description |
|---|---|---|
out |
Required | Output directory should match with protoc out field |
mode |
per_service |
per_service or per_method |
impl_suffix |
_handler |
Suffix for implementation files |
dir_pattern |
"" |
Directory pattern with placeholders |
| Placeholder | Expands to | Example |
|---|---|---|
{package} |
Full proto package | test.v1 |
{package_path} |
Package with / |
test/v1 |
{service} |
Service name | TestService |
{service_snake} |
snake_case service | test_service |
For a service like:
package test.v1; service TestService { rpc Echo(EchoRequest) returns (EchoResponse); }
Generates:
test_service_handler.gen.go- Interface definition (regenerated)test_service_handler.go- Struct + method implementations (safe to edit)
Generates:
test_service_handler.gen.go- Interface definition (regenerated)test_service_handler.go- Struct only (safe to edit)test_service_echo.go- Echo method implementation (safe to edit)
# Day 0: Initial generation buf generate # Edit the generated handler files, add your logic # Day N: Proto changes (new RPC added) buf generate # Only new RPC stubs are added, existing code untouched
See the example/ directory for a complete working example with:
- Proto service definition
- buf.gen.yaml configuration for both modes
- Generated output examples
- Go 1.27+
- protoc
- buf (recommended) or protoc with plugins
go build ./cmd/protoc-gen-connect-go-handler
go test ./... -vcd example
buf generate