The lazydone
package provides a lazy initialized done channel with a valid zero value in Go.
package main
import (
"fmt"
"time"
"fillmore-labs.com/lazydone"
)
type Result struct {
lazydone.Lazy
value int
}
func main() {
var result Result
go func() {
time.Sleep(100 * time.Millisecond)
result.value = 42
result.Close() // The result is ready.
}()
<-result.Done() // Wait for the result.
fmt.Println("Value:", result.value)
}
The channel can be used in select
statements, which is not possible with
Mutexes or WaitGroups.