~/talk

Useful Functional-Options Tricks for Better Libraries

Talkat GopherCon Europe 2023 · 2023-06-26..06-29· [developer-experience, go]
links:none on file
slides:none on file

Functional options in Go become a practical library design recipe: use a custom functional options type, apply options to an internal, non‑exported config struct, validate once and return errors (bundle multiple failures with Go 1.20 multierror), guarantee option order insensitivity, and make the produced middleware effectively immutable. Avoid hot‑path use for performance reasons. Enforce compile‑time safety for CORS by splitting factories into OptionAnon/OptionCred and use an opaque interface plus internal/risky packages and type aliases to surface only safe options.

AI-written summary · about

  1. 0:00Functional options pattern
  2. 2:50Why functional options improve design
  3. 4:14Problems with naive approaches
  4. 7:22Functional options for expressiveness
  5. 8:45When not to use functional options
  6. 9:47Factory functions fail safely
  7. 12:16Order insensitivity in options
  8. 13:17Handling repeated option calls
  9. 14:43Immutability in middleware
  10. 16:09Benefits of custom option types
  11. 18:16Avoiding mutable behavior
  12. 19:00Intermediate non-exported structs
  13. 20:24Options as opaque interfaces
  14. 21:27Reducing boilerplate with adapters
  15. 22:33Enforcing constraints to prevent misuse
  16. 24:18Splitting options by access
  17. 25:22Advanced options in separate packages
  18. 26:25Summary and recommendations
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.