This repository has been archived on 2022-03-03. You can view files and clone it, but cannot push or open issues/pull-requests.
opt/opt.go

25 lines
523 B
Go

package opt
// Func is a functional option for A
type Func[A any] func(*A)
// Apply applies all options to A
func Apply[A any](a *A, opts ...Func[A]) {
for _, opt := range opts {
opt(a)
}
}
// ErrorFunc is a functional option for A that can return an error
type ErrorFunc[A any] func(*A) error
// ApplyError applies all options to A, returning the first error
func ApplyError[A any](a *A, opts ...ErrorFunc[A]) error {
for _, opt := range opts {
if err := opt(a); err != nil {
return err
}
}
return nil
}