Ai
1 Star 0 Fork 0

sy_183/go-common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
elem.go 363 Bytes
一键复制 编辑 原始数据 按行查看 历史
sy_183 提交于 2023-07-20 13:21 +08:00 . 1. 增加了 lifecycle 泛型属性的函数
package component
import "sync"
type Elem[E any] struct {
Init func() E
initializer sync.Once
done bool
elem E
}
func NewElem[E any](init func() E) *Elem[E] {
return &Elem[E]{Init: init}
}
func (c *Elem[E]) Get() E {
if c.done {
return c.elem
}
c.initializer.Do(func() {
c.elem = c.Init()
c.done = true
})
return c.elem
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sy_183/go-common.git
git@gitee.com:sy_183/go-common.git
sy_183
go-common
go-common
v1.0.4

搜索帮助