Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Lazy ¶
type Lazy struct {
// contains filtered or unexported fields
}
Lazy is a lazily initialized done channel. The zero value for a Lazy is valid and can be closed. A Lazy must not be copied after first use.
Example ¶
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.
}()
fmt.Println("SafeResult:", &result.Lazy)
select {
case <-result.Done():
fmt.Println("Already done")
default:
fmt.Println("Still processing...")
}
<-result.Done() // Wait for the result.
fmt.Println("SafeResult:", &result.Lazy)
fmt.Println("Value:", result.value)
}
Output: SafeResult: pending Still processing... SafeResult: done Value: 42
func (*Lazy) Close ¶
func (l *Lazy) Close()
Close closes the done channel. You shouldn't close the channel twice.
func (*Lazy) Done ¶
func (l *Lazy) Done() <-chan struct{}
Done returns the done channel.
Example ¶
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.
}()
fmt.Println("SafeResult:", &result.Lazy)
<-result.Done() // Wait for the result.
fmt.Println("SafeResult:", &result.Lazy)
fmt.Println("Value:", result.value)
}
Output: SafeResult: pending SafeResult: done Value: 42
type SafeLazy ¶
type SafeLazy struct {
// contains filtered or unexported fields
}
SafeLazy is a lazily initialized done channel. It is an alternate implementation of Lazy using sync/atomic.Pointer. The zero value for a SafeLazy is valid and can be closed. A SafeLazy must not be copied after first use.
Example ¶
package main
import (
"fmt"
"time"
"fillmore-labs.com/lazydone"
)
type SafeResult struct {
lazydone.SafeLazy
value int
}
func main() {
var result SafeResult
go func() {
time.Sleep(100 * time.Millisecond)
result.value = 42
result.Close() // The result is ready.
}()
fmt.Println("SafeResult:", &result.SafeLazy)
select {
case <-result.Done():
fmt.Println("Already done")
default:
fmt.Println("Still processing...")
}
<-result.Done() // Wait for the result.
fmt.Println("SafeResult:", &result.SafeLazy)
fmt.Println("Value:", result.value)
}
Output: SafeResult: pending Still processing... SafeResult: done Value: 42
func (*SafeLazy) Close ¶
func (l *SafeLazy) Close()
Close closes the done channel. You shouldn't close the channel twice.
func (*SafeLazy) Done ¶
func (l *SafeLazy) Done() <-chan struct{}
Done returns the done channel.
Example ¶
package main
import (
"fmt"
"time"
"fillmore-labs.com/lazydone"
)
type SafeResult struct {
lazydone.SafeLazy
value int
}
func main() {
var result SafeResult
go func() {
time.Sleep(100 * time.Millisecond)
result.value = 42
result.Close() // The result is ready.
}()
fmt.Println("SafeResult:", &result.SafeLazy)
<-result.Done() // Wait for the result.
fmt.Println("SafeResult:", &result.SafeLazy)
fmt.Println("Value:", result.value)
}
Output: SafeResult: pending SafeResult: done Value: 42
Click to show internal directories.
Click to hide internal directories.