-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonce.go
More file actions
28 lines (24 loc) · 761 Bytes
/
once.go
File metadata and controls
28 lines (24 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Package once exists because sync.Once is annoying to use because each place you
// use it must provide the function to execute. That's great if you want to do
// different things, but if you always want to do the same thing, it's repetitive.
//
// Further, ReliableError provides a way to use Once to make go-routines that return
// error easy to write safely.
package once
import (
"sync"
)
type Once struct {
o sync.Once
f func()
}
// New provides the function that Do will will call. The function
// will only be called once no matter how many times Do is invoked.
func New(f func()) *Once {
return &Once{f: f}
}
// Call the function provided in New. Calling Do on an uninitialized
// Once object will panic.
func (o *Once) Do() {
o.o.Do(o.f)
}