38 lines
634 B
Markdown
38 lines
634 B
Markdown
|
# 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:
|
||
|
```go
|
||
|
type Foo struct {
|
||
|
Bar string
|
||
|
}
|
||
|
|
||
|
func WithBar(b string) opt.Func[Foo] {
|
||
|
return func(f *Foo) {
|
||
|
f.Bar = b
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
With errors:
|
||
|
```go
|
||
|
type Foo struct {
|
||
|
Bar string
|
||
|
}
|
||
|
|
||
|
func WithBar(b string) opt.ErrorFunc[Foo] {
|
||
|
return func(f *Foo) error {
|
||
|
f.Bar = b
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## License
|
||
|
|
||
|
[MIT](LICENSE)
|
||
|
|
||
|
Credit to `@segfaultax` in the [Discord Gophers](https://discord.gg/golang) server for the idea and starting implementation.
|