Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • 9
    Sources : commandcenter.blogspot.com.au/2014/01/… , dave.cheney.net/2014/10/17/functional-options-for-friendly-apis Commented Nov 9, 2014 at 16:17
  • 48
    Clever, but too complicated. The philosophy of Go is to write code in a straightforward way. Just pass a struct and test for default values. Commented Jan 30, 2016 at 23:23
  • 25
    Just FYI, the original author of this idiom, at at least the first publisher referenced, is Commander Rob Pike, whom I consider authoritative enough for Go philosophy. Link - commandcenter.blogspot.bg/2014/01/…. Also search for "Simple is complicated". Commented Oct 31, 2016 at 16:11
  • Upvoted long ago, but you write: provide convenient ways for your user to use or create standard options. If FooBar is an opaque struct type, I don't see how users could create additional options. If FooBar isn't opaque, then the benefits of functional options is moot, because users now have two ways of initialising a FooBar: via a struct literal or via the factory function ("constructor"). My conclusion is that optional options work best when the set of options is meant to be closed and controlled by the package author. Commented Oct 24, 2022 at 11:26
  • 2
    This trick may have been invented by Rob Pike, but that doesn't mean it's not an abomination to work around a language deficiency. IMHO this technique is way more convoluted, complicated, and confusing than the worst abuses of optional arguments that I've ever seen. Commented Sep 3, 2023 at 23:15