Discover Packages
github.com/goexts/generic
settings
package
Version:
v0.1.0
Opens a new window with list of versions in this module.
Published: Oct 28, 2024
License: MIT
Opens a new window with license information.
Imports: 0
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
README
¶
Settings
This is a simple example of using generics in Go.
Example
type Setting = settings.Setting[Serialize]
func WithValue(value string) Setting {
return func(o *Serialize) {
o.Value = value
}
}
type Serialize struct {
Value string
}
var (
defaultSerialize = Serialize{
Value: "hello",
}
func NewSerialize(ts ...Setting) *Serialize {
serialize := settings.Apply(&defaultSerialize, ts)
return serialize
}
func main() {
serialize = NewSerialize()
fmt.Println(serialize.Value)
// Output: hello
serialize := NewSerialize(WithValue("world"))
fmt.Println(serialize.Value)
// Output: world
}
Expand ▾
Collapse ▴
Documentation
¶
func Apply[S any ](s *S, fs []func(*S)) *S
Apply is apply settings
func ApplyAny[S any ](s *S, fs []interface{}) *S
ApplyAny Applies a set of setting functions to a struct.
These Settings functions can be ApplyFunc[S] or func(*S), or objects that implement the ApplySetting interface.
func ApplyOr[S any ](s *S, fs ...func(*S)) *S
ApplyOr is an apply settings with defaults
func ApplyOrZero[S any ](fs ...func(*S)) *S
ApplyOrZero is an apply settings with defaults
type ApplyFunc[S any ] func(*S)
ApplyFunc is a ApplyFunc function for Apply
type ApplySetting[S any ] interface {
Apply(v *S)
}
Source Files
¶
Click to show internal directories.
Click to hide internal directories.