1 Star 0 Fork 0

h79 / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
option.go 810 Bytes
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-11-30 16:46 . BUG
package option
type Option interface {
String() string
Type() int
Value() interface{}
}
type Opt[T any] struct {
v T
d string
t int
}
func WithOpt[T any](v T, desc string, t int) Option {
return &Opt[T]{v: v, d: desc, t: t}
}
func (o Opt[T]) String() string {
return o.d
}
func (o Opt[T]) Type() int { return o.t }
func (o Opt[T]) Value() interface{} { return o.v }
func Filter(filter func(opt Option) bool, opts ...Option) {
for i := range opts {
if filter(opts[i]) {
return
}
}
}
func Exist(ty int, opts ...Option) (Option, bool) {
for i := range opts {
if opts[i].Type() == ty {
return opts[i], true
}
}
return nil, false
}
func Select[T any](t int, defValue T, opts ...Option) T {
if r, ok := Exist(t, opts...); ok {
return r.Value().(T)
}
return defValue
}
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.57

搜索帮助