This repository has been archived on 2022-03-03. You can view files and clone it, but cannot push or open issues/pull-requests.
Go to file
jolheiser b985c9ff0b
Initial commit
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2022-03-03 14:50:36 -06:00
.gitignore Initial commit 2022-03-03 14:50:36 -06:00
LICENSE Initial commit 2022-03-03 14:50:36 -06:00
README.md Initial commit 2022-03-03 14:50:36 -06:00
go.mod Initial commit 2022-03-03 14:50:36 -06:00
go.sum Initial commit 2022-03-03 14:50:36 -06:00
opt.go Initial commit 2022-03-03 14:50:36 -06:00
opt_example_test.go Initial commit 2022-03-03 14:50:36 -06:00
opt_test.go Initial commit 2022-03-03 14:50:36 -06:00

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

MIT

Credit to @segfaultax in the Discord Gophers server for the idea and starting implementation.