Home

opt @main - refs - log -
-
https://git.jolheiser.com/opt.git
Functional options
opt / README.md
- raw -
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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.