option

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 0 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[T any](t *T, opts ...Option[T])

Apply 将 opts 应用在 t 之上

Example
u := &User{}
Apply[User](u, WithName("Tom"), WithAge(18))
fmt.Println(u)
Output:

&{Tom 18}

func ApplyErr

func ApplyErr[T any](t *T, opts ...OptionErr[T]) error

ApplyErr 形如 Apply,它将 opts 应用在 t 之上, 如果 opts 中任何一个返回 error,那么它会中断并且返回 error

Example
u := &User{}
err := ApplyErr[User](u, WithNameErr("Tom"), WithAgeErr(18))
fmt.Println(err)
fmt.Println(u)

err = ApplyErr[User](u, WithNameErr(""), WithAgeErr(18))
fmt.Println(err)
Output:

<nil>
&{Tom 18}
name 不能为空

Types

type Option

type Option[T any] func(t *T)

Option 是用于 Option 模式的泛型设计, 避免在代码中定义很多类似这样的结构体 一般情况下 T 应该是一个结构体

type OptionErr

type OptionErr[T any] func(t *T) error

OptionErr 形如 Option,但是会返回一个 error 你应该优先使用 Option,除非你在设计 option 模式的时候需要进行一些校验

Jump to

Keyboard shortcuts

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