Documentation
¶
Overview ¶
Package options provides a generic mechanism for defining configuration of arbitrary types.
Example ¶
package main
import (
"fmt"
"github.com/ava-labs/libevm/libevm/options"
)
// config is an arbitrary type to be configured with [options.Option] values.
// Although it can be exported, there is typically no need.
type config struct {
num int
flag bool
}
// An Option configures an arbitrary type. Using a type alias (=) instead of a
// completely new type is recommended as it maintains compatibility with helpers
// such as [options.Func].
type Option = options.Option[config]
func WithNum(n int) Option {
return options.Func[config](func(c *config) {
c.num = n
})
}
func WithFlag(b bool) Option {
return options.Func[config](func(c *config) {
c.flag = b
})
}
func main() {
num42 := WithNum(42)
flagOn := WithFlag(true)
// Some IDEs and linters might complain about the redundant type parameter
// as it can be inferred, but it makes for more readable code.
fromZero := options.As[config](num42, flagOn)
fmt.Printf("From zero value: %T(%+[1]v)\n", fromZero)
fromDefault := options.ApplyTo(&config{
num: 100,
}, flagOn)
fmt.Printf("Applied to default value: %T(%+[1]v)\n", fromDefault)
}
Output: From zero value: *options_test.config(&{num:42 flag:true}) Applied to default value: *options_test.config(&{num:100 flag:true})
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
Click to show internal directories.
Click to hide internal directories.