Documentation
¶
Overview ¶
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.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
func ReliableError ¶
ReliableError provides a way to guarantee that a go routine returns an error over a channel, even if there is a panic. This is important because a common idiom is to wait for a go-routine to finish by reading a error from a channel.
Without something like ReliableError, that is unsafe.
The other idiom, to use a sync.WaitGroup and then defer wg.Done() doesn't provide a reliable way to return an error. If you're setting an error value, that error may not be set if the go-routine panics.
Example:
echan := make(chan error) go func() { var err error re := ReliableError(echan, &err) defer re.Catch() ... do stuff err = something() if err != nil { re.Do() return } ... repeat as needed }() err :- <-echan