From cf0df4c1c827db0b2e733f851300cf8c3567ba18 Mon Sep 17 00:00:00 2001 From: OneOfOne Date: Sat, 8 Oct 2022 14:02:43 -0500 Subject: [PATCH] Once with ret based on golang/go#56102 --- sync.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 sync.go 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 +}