settings

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 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[T Setting[S], S any](d *S, ss []T) *S

Apply is apply settings

func ApplyOr added in v0.0.4

func ApplyOr[T Setting[S], S any](d S, ss ...T) *S

ApplyOr is an apply settings with defaults

func ApplyOrZero

func ApplyOrZero[T Setting[S], S any](ss ...T) *S

ApplyOrZero is an apply settings with defaults

Types

type Setting

type Setting[S any] func(*S)

Setting is a setting function for Apply

Jump to

Keyboard shortcuts

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