jolheiser
b985c9ff0b
Signed-off-by: jolheiser <john.olheiser@gmail.com> |
||
---|---|---|
.gitignore | ||
LICENSE | ||
README.md | ||
go.mod | ||
go.sum | ||
opt.go | ||
opt_example_test.go | ||
opt_test.go |
README.md
opt
opt
is a tiny package that reduces boilerplate for functional options.
Simply decide whether your options can return errors or not, then use the corresponding type.
Without errors:
type Foo struct {
Bar string
}
func WithBar(b string) opt.Func[Foo] {
return func(f *Foo) {
f.Bar = b
}
}
With errors:
type Foo struct {
Bar string
}
func WithBar(b string) opt.ErrorFunc[Foo] {
return func(f *Foo) error {
f.Bar = b
return nil
}
}
License
Credit to @segfaultax
in the Discord Gophers server for the idea and starting implementation.