Skip to content

Commit

Permalink
Once with ret based on golang/go#56102
Browse files Browse the repository at this point in the history
  • Loading branch information
OneOfOne committed Oct 8, 2022
1 parent 3ece510 commit cf0df4c
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions sync.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit cf0df4c

Please sign in to comment.