Menu

1 / 2

Functional Options vs Builder vs Config Struct in Go: Pick One

DEV Community·Gabriel Anhaia·6 months ago
#xH65KlFL
#option#go#programming#time#server
Reading 0:00
15s threshold

Book: The Complete Guide to Go Programming

Also by me: Thinking in Go (2-book series) — Complete Guide to Go Programming + Hexagonal Architecture in Go

My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools

Me: xgabriel.com | GitHub

You have a NewServer constructor. Today it takes one argument: an address. Three sprints from now it takes nine. Read timeout, write timeout, max connections, TLS config, logger, metrics sink, request ID generator, idle timeout, and a hook for graceful shutdown.

The first version was a positional parameter list. By argument three the call site stopped being readable. By argument five it is the kind of bug where somebody passes 5*time.Second where 30*time.Second was wanted, and a payment service times out for a weekend before its database does.

Go gives you three patterns for this and they are not equivalent. Functional options, the builder pattern, and a config struct each solve a different version of the same problem.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.