GeistHaus
log in · sign up

Elegant Configuration APIs and Command Line Flags

matttproud.com

I’d like to share a design pattern with Go that I really find particularly elegant. It orients itself around hermetic, testable configuration using command line flags. But before I do, I need to put three questions to you since they determine if you need to do some pre-reading in advance. If you are familiar with package flag, are you aware of the Var-suffixed flag creation API variants (e.g., flag.BoolVar to complement flag.Bool)? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // https://cs.opensource.google/go/go/+/refs/tags/go1.24.2:src/flag/flag.go package flag // Bool defines a bool flag with specified name, default value, and usage string. // The return value is the address of a bool variable that stores the value of the flag. func Bool(name string, value bool, usage string) *bool { return CommandLine.Bool(name, value, usage) } // BoolVar defines a bool flag with specified name, default value, and usage string. // The argument p points to a bool variable in which to store the value of the flag. func BoolVar(p *bool, name string, value bool, usage string) { CommandLine.Var(newBoolValue(value, p), name, usage) } If you are aware of the Var variants, do you why these variants even exist?

1 page links to this URL
goldentest

Package goldentest makes it easy to build tests that compare outputs (got) that come from a system under test (SUT) against version control-managed reference files (want) and facilitate the management of the reference files (want) as SUT behavior changes.