settings

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2024 License: MIT Imports: 0 Imported by: 0

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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[S any](s *S, fs []func(*S)) *S

Apply is apply settings

func ApplyAny added in v0.0.9

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 added in v0.0.4

func ApplyOr[S any](s *S, fs ...func(*S)) *S

ApplyOr is an apply settings with defaults

func ApplyOrZero

func ApplyOrZero[S any](fs ...func(*S)) *S

ApplyOrZero is an apply settings with defaults

Types

type ApplyFunc added in v0.0.9

type ApplyFunc[S any] func(*S)

ApplyFunc is a ApplyFunc function for Apply

func (ApplyFunc[S]) Apply added in v0.0.9

func (s ApplyFunc[S]) Apply(v *S)

type ApplySetting added in v0.0.9

type ApplySetting[S any] interface {
	Apply(v *S)
}

type Setting

type Setting[S any] interface {
	func(*S) | ApplyFunc[S]
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL