chann

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2022 License: MIT Imports: 4 Imported by: 2

README

chann example workflow

a unified channel package in Go

import "golang.design/x/chann"

This package requires Go 1.18.

Basic Usage

Different types of channels:

ch := chann.New[int]()                  // unbounded, capacity unlimited
ch := chann.New[func()](chann.Cap(0))   // unbufferd, capacity 0
ch := chann.New[string](chann.Cap(100)) // buffered,  capacity 100

Send and receive operations:

ch.In() <- 42
println(<-ch.Out()) // 42

Close operation:

ch.Close()

Channel properties:

ch.Len() // the length of the channel
ch.Cap() // the capacity of the channel

See https://golang.design/research/ultimate-channel for more details of the motivation of this abstraction.

License

MIT | © 2021 The golang.design Initiative Authors, written by Changkun Ou.

Documentation

Overview

Package chann providesa a unified channel package.

The package is compatible with existing buffered and unbuffered channels. For example, in Go, to create a buffered or unbuffered channel, one uses built-in function `make` to create a channel:

ch := make(chan int)     // unbuffered channel
ch := make(chan int, 42) // or buffered channel

However, all these channels have a finite capacity for caching, and it is impossible to create a channel with unlimited capacity, namely, an unbounded channel.

This package provides the ability to create all possible types of channels. To create an unbuffered or a buffered channel:

ch := chann.New[int](chann.Cap(0))  // unbuffered channel
ch := chann.New[int](chann.Cap(42)) // or buffered channel

More importantly, when the capacity of the channel is unspecified, or provided as negative values, the created channel is an unbounded channel:

ch := chann.New[int]()               // unbounded channel
ch := chann.New[int](chann.Cap(-42)) // or unbounded channel

Furthermore, all channels provides methods to send (In()), receive (Out()), and close (Close()).

Note that to close a channel, must use Close() method instead of the language built-in method Two additional methods: ApproxLen and Cap returns the current status of the channel: an approximation of the current length of the channel, as well as the current capacity of the channel.

See https://golang.design/research/ultimate-channel to understand the motivation of providing this package and the possible use cases with this package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fanout

func Fanout[T any](randomizer func(max int) int, in *Chann[T], outs ...*Chann[T])

Fanout provides a generic fan-out functionality for variadic channels.

func LB

func LB[T any](randomizer func(max int) int, ins []*Chann[T], outs []*Chann[T])

LB load balances the given input channels to the output channels.

func Ranger

func Ranger[T any]() (*Sender[T], *Receiver[T])

Ranger returns a Sender and a Receiver. The Receiver provides a Next method to retrieve values. The Sender provides a Send method to send values and a Close method to stop sending values. The Next method indicates when the Sender has been closed, and the Send method indicates when the Receiver has been freed.

This is a convenient way to exit a goroutine sending values when the receiver stops reading them.

Types

type Chann

type Chann[T any] struct {
	// contains filtered or unexported fields
}

Chann is a generic channel abstraction that can be either buffered, unbuffered, or unbounded. To create a new channel, use New to allocate one, and use Cap to configure the capacity of the channel.

func Fanin

func Fanin[T any](chans ...*Chann[T]) *Chann[T]

Fanin provides a generic fan-in functionality for variadic channels.

func New

func New[T any](opts ...Opt) *Chann[T]

New returns a Chann that may represent a buffered, an unbuffered or an unbounded channel. To configure the type of the channel, one may pass Cap as the argument of this function.

By default, or without specification, the function returns an unbounded channel which has unlimited capacity.

	ch := chann.New[float64]()
	// or
 ch := chann.New[float64](chann.Cap(-1))

If the chann.Cap specified a non-negative integer, the returned channel is either unbuffered (0) or buffered (positive).

Note that although the input arguments are specified as variadic parameter list, however, the function panics if there is more than one option is provided.

Example
package main

import (
	"fmt"

	"golang.design/x/chann"
)

func main() {
	ch := chann.New[int]()

	go func() {
		for i := 0; i < 10; i++ {
			ch.In() <- i // never block
		}
		ch.Close()
	}()

	sum := 0
	for i := range ch.Out() {
		sum += i
	}
	fmt.Println(sum)
}
Output:

45

func (*Chann[T]) Cap

func (ch *Chann[T]) Cap() int

Cap returns the capacity of the channel.

func (*Chann[T]) Close

func (ch *Chann[T]) Close()

Close closes the channel gracefully.

func (*Chann[T]) In

func (ch *Chann[T]) In() chan<- T

In returns the send channel of the given Chann, which can be used to send values to the channel. If one closes the channel using close(), it will result in a runtime panic. Instead, use Close() method.

func (*Chann[T]) Len

func (ch *Chann[T]) Len() int

Len returns an approximation of the length of the channel.

Note that in a concurrent scenario, the returned length of a channel may never be accurate. Hence the function is named with an Approx prefix.

func (*Chann[T]) Out

func (ch *Chann[T]) Out() <-chan T

Out returns the receive channel of the given Chann, which can be used to receive values from the channel.

type Opt

type Opt func(*config)

Opt represents an option to configure the created channel. The current possible option is Cap.

func Cap

func Cap(n int) Opt

Cap is the option to configure the capacity of a creating buffer. if the provided number is 0, Cap configures the creating buffer to a unbuffered channel; if the provided number is a positive integer, then Cap configures the creating buffer to a buffered channel with the given number of capacity for caching. If n is a negative integer, then it configures the creating channel to become an unbounded channel.

type Receiver

type Receiver[T any] struct {
	// contains filtered or unexported fields
}

A Receiver receives values from a Sender.

func (*Receiver[T]) Next

func (r *Receiver[T]) Next() (T, bool)

Next returns the next value from the channel. The bool result indicates whether the value is valid, or whether the Sender has been closed and no more values will be received.

type Sender

type Sender[T any] struct {
	// contains filtered or unexported fields
}

A sender is used to send values to a Receiver.

func (*Sender[T]) Close

func (s *Sender[T]) Close()

Close tells the receiver that no more values will arrive. After Close is called, the Sender may no longer be used.

func (*Sender[T]) Send

func (s *Sender[T]) Send(v T) bool

Send sends a value to the receiver. It returns whether any more values may be sent; if it returns false the value was not sent.

Jump to

Keyboard shortcuts

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