From ae85fab3c378726e84bc362effde97606eab0a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 12 May 2021 03:28:46 +0200 Subject: [PATCH] Document functional options (#1899) * Document functional options * Update CONTRIBUTING.md Co-authored-by: Tyler Yahn * Update CONTRIBUTING.md Co-authored-by: Tyler Yahn Co-authored-by: Tyler Yahn Co-authored-by: Anthony Mirabella --- CONTRIBUTING.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f11608b5e..83ea23b1c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -294,6 +294,23 @@ func WithMyType(t MyType) Option { } ``` +##### Functional Options + +```go +type optionFunc func(*config) + +func (fn optionFunc) apply(c *config) { + fn(c) +} + +// WithMyType sets t as MyType. +func WithMyType(t MyType) Option { + return optionFunc(func(c *config) { + c.MyType = t + }) +} +``` + #### Instantiation Using this configuration pattern to configure instantiation with a `NewT`