The Generic Protocol Pattern in Go: Designing Extensible CLI Interfaces
June 24, 2025
In systems programming and CLI tool design, a consistent and extensible protocol can make or break maintainability. Whether you’re building a REPL, a network service, or an internal CLI for scripting, the Generic Protocol Pattern helps you separate commands, parsers, and handlers.
This post introduces the pattern and demonstrates how to build a robust protocol interpreter in Go โ one line at a time.
๐งฉ What Is the Generic Protocol Pattern?
Itโs a design pattern for stream-based command interpreters that:
Accept string-based commands via stdin, socket, or pipe
Parse input into structured messages
Delegate logic to handlers
Produce line-based responses
It’s widely used in:
Redis CLI protocol
SMTP, FTP, and IMAP
Debuggers and scripting engines
REPLs and interactive shells
๐ง Go Interface Design
We define a generic Command interface:
1
2
3
typeCommandinterface{Name()string}
And a Handler interface:
1
2
3
typeHandlerinterface{Handle(cmdCommand)string}
Then we use a Dispatcher to wire command names to handlers:
โ Extensibility
New commands are easy to add โ define a struct, implement a handler, register it.
โ Separation of Concerns
Parsing, routing, and handling are cleanly isolated.
โ Stream-Friendly
Ideal for REPLs, socket-based daemons, and interactive protocols.
โ Testable
Handlers and parsers can be independently unit-tested.
๐ Conclusion
The Generic Protocol Pattern is an underappreciated gem in systems programming. Whether you’re building an internal tool or network protocol, this approach provides a clean, extensible foundation with zero dependencies.
๐ก Tip: Combine this with custom binary formats, caching, or memory pooling for serious performance wins.
๐ Follow me on norbix.dev for more insights on Go, system design, and engineering wisdom.