Skip to content

a unified channel package for buffered, unbuffered, and unbounded channels

License

Notifications You must be signed in to change notification settings

golang-design/chann

Repository files navigation

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.