Documentation
¶
Overview ¶
Package option provides a set of options for the client.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
Apply applies a slice of functions to a value.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/option"
)
type Server struct {
Name string
}
func WithName(p string) option.Option[*Server] {
return func(s *Server) {
s.Name = p
}
}
func main() {
s := &Server{}
option.Apply(
s,
WithName("localhost"),
)
fmt.Printf("%s\n", s.Name)
}
Output: localhost
func ApplyE ¶
ApplyE applies a slice of error-returning functions to a value and stops on the first encountered error.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/foomo/go/option"
)
type Server struct {
Name string
}
func WithNameE(p string) option.OptionE[*Server] {
return func(s *Server) error {
if len(p) == 0 {
return errors.New("invalid name")
}
s.Name = p
return nil
}
}
func main() {
s := &Server{}
err := option.ApplyE(
s,
WithNameE("localhost"),
)
fmt.Println(err)
fmt.Println(s.Name)
}
Output: <nil> localhost
Types ¶
type Builder ¶ added in v0.3.0
Builder is a generic option builder that collects functional options.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/option"
)
type MyOptions struct {
Name string
}
type MyOptionsBuilder struct {
option.Builder[*MyOptions]
}
func MyBuilder() *MyOptionsBuilder {
return &MyOptionsBuilder{
option.Builder[*MyOptions]{},
}
}
func (b *MyOptionsBuilder) WithName(name string) *MyOptionsBuilder {
b.Opts = append(b.Opts, func(o *MyOptions) {
o.Name = name
})
return b
}
func main() {
b := MyBuilder()
b.WithName("example")
o := MyOptions{}
option.Apply(&o, b.List()...)
fmt.Println(o)
}
Output: {example}
Click to show internal directories.
Click to hide internal directories.