lazydone

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 2 Imported by: 1

README

Fillmore Labs LazyDone

Go Reference Test Coverage Maintainability Go Report Card License

The lazydone package provides a lazy initialized done channel with a valid zero value in Go.

Usage

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.

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) Closed

func (l *Lazy) Closed() bool

Closed returns true if the done channel is closed.

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

func (*Lazy) String

func (l *Lazy) String() string

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) Closed

func (l *SafeLazy) Closed() bool

Closed returns true if the done channel is closed.

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

func (*SafeLazy) String

func (l *SafeLazy) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL