diff --git a/sync.go b/sync.go new file mode 100644 index 0000000..e96da1e --- /dev/null +++ b/sync.go @@ -0,0 +1,14 @@ +package genh + +import "sync" + +type Once[T any] struct { + once sync.Once + v T + err error +} + +func (o *Once[T]) Do(fn func() (T, error)) (v T, err error) { + o.once.Do(func() { o.v, o.err = fn() }) + return o.v, o.err +}