CQRS Pattern Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓CQRS Pattern runs in O(1) time and O(n) space, in Go as in every other language.
- ✓Nothing. An interface plus a type switch is the dispatcher, and the language provides no registry, no generics-based lookup and no exhaustiveness checking.
- ✓Seal the command interface with an unexported marker method, so only types declared in your own package can satisfy it.
- ✓The Go trap: Go has no exhaustiveness checking of any kind, so adding a command type and forgetting to add its case falls through to the default branch at run time, in production, with whatever that branch happens to do.
- ✓A type switch resolves through the interface table, which is a hash lookup rather than the jump table an enum match compiles to, so it is cheap but not free.
What CQRS Pattern actually is
CQRS Pattern in Go: what you actually reach for
Go implementation
package orders
import "fmt"
// A SEALED interface: the unexported marker method means only types in
// THIS package can implement Command.
type Command interface {
isCommand()
}
type CreateOrder struct {
OrderID string
Items []string
}
type CancelOrder struct {
OrderID string
Reason string
}
type ShipOrder struct {
OrderID string
Carrier string
}
func (CreateOrder) isCommand() {}
func (CancelOrder) isCommand() {}
func (ShipOrder) isCommand() {}
// Go has NO exhaustiveness check. Adding a command type and forgetting
// its case here compiles cleanly and lands in default at RUN TIME, in
// production. The sealed interface constrains who can implement; it does
// not make this switch complete. An external linter ("exhaustive") is
// the only mechanical check available.
func Handle(cmd Command) (string, error) {
switch c := cmd.(type) {
case CreateOrder:
return fmt.Sprintf("order_created:%s", c.OrderID), nil
case CancelOrder:
return fmt.Sprintf("order_cancelled:%s", c.OrderID), nil
case ShipOrder:
return fmt.Sprintf("order_shipped:%s", c.OrderID), nil
default:
// Make the hole LOUD, since the compiler will not.
return "", fmt.Errorf("unhandled command type %T", cmd)
}
}
// An interface holding a NIL POINTER is not itself nil:
// var p *CreateOrder
// var c Command = p
// c == nil -> FALSE
// so a nil check at the interface passes and the handler dereferences nil.Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for CQRS Pattern?
Nothing. An interface plus a type switch is the dispatcher, and the language provides no registry, no generics-based lookup and no exhaustiveness checking.
What is the most common Go mistake when implementing CQRS Pattern?
Go has no exhaustiveness checking of any kind, so adding a command type and forgetting to add its case falls through to the default branch at run time, in production, with whatever that branch happens to do. The sealed-interface trick constrains who can implement the interface but does nothing to make the switch complete, and the only mechanical check available is an external linter such as exhaustive. The second trap is subtler and specific to Go interfaces: an interface value holding a nil pointer is not itself nil, so a command variable compared against nil can pass the check while dereferencing to a nil pointer inside the handler. Compare against nil at the concrete type, not at the interface.
How does CQRS Pattern actually perform in Go?
A type switch resolves through the interface table, which is a hash lookup rather than the jump table an enum match compiles to, so it is cheap but not free. Command structs passed by value avoid an allocation; passing pointers means the escape analysis usually moves them to the heap. Time complexity is O(1) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Devin, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.