You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
jolheiser b985c9ff0b
Initial commit
Signed-off-by: jolheiser <john.olheiser@gmail.com>
1 year ago
.gitignore Initial commit 1 year ago
LICENSE Initial commit 1 year ago
README.md Initial commit 1 year ago
go.mod Initial commit 1 year ago
go.sum Initial commit 1 year ago
opt.go Initial commit 1 year ago
opt_example_test.go Initial commit 1 year ago
opt_test.go Initial commit 1 year ago

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.