chanplus

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChannelState

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

ChannelState represents a tracked channel with introspection capabilities.

func New

func New[T any](capacity int) *ChannelState[T]

New creates and returns a new Chan instance with the specified capacity. If capacity is 0, an unbuffered channel is created.

Example:

ch := chanplus.New[int](3) // Creates a buffered channel of integers with capacity 3
unbufferedCh := chanplus.New[string](0) // Creates an unbuffered channel of strings

func (*ChannelState[T]) Cap

func (cs *ChannelState[T]) Cap() int

Cap returns the capacity of the channel. This method is thread-safe.

Example:

ch := chanplus.New[int](5)
fmt.Println("Capacity:", ch.Cap()) // Output: 5

func (*ChannelState[T]) Close

func (cs *ChannelState[T]) Close()

Close closes the channel. It returns an error if the channel is already closed. This method is thread-safe.

Example:

ch := chanplus.New[int](1)
err := ch.Close()
if err != nil {
    // Handle error (e.g., channel already closed)
}

func (*ChannelState[T]) GetChannel

func (cs *ChannelState[T]) GetChannel() chan T

func (*ChannelState[T]) IsClosed

func (cs *ChannelState[T]) IsClosed() bool

IsClosed returns true if the channel is closed, false otherwise. This method is thread-safe.

Example:

ch := chanplus.New[int](1)
fmt.Println("Is closed:", ch.IsClosed())
ch.Close()
fmt.Println("Is closed:", ch.IsClosed())

func (*ChannelState[T]) Len

func (cs *ChannelState[T]) Len() int

Len returns the number of items currently in the channel. This method is thread-safe.

Example:

ch := chanplus.New[int](5)
fmt.Println("Length:", ch.Len()) // Output: 0

func (*ChannelState[T]) Receive

func (cs *ChannelState[T]) Receive() (T, bool)

Receive receives a value from the channel. It returns the received value and a boolean indicating whether the channel is closed. This method is thread-safe.

Example:

ch := chanplus.New[int](1)
ch.Send(42)
value, ok := ch.Receive()
if !ok {
    // Channel is closed
} else {
    fmt.Println("Received:", value)
}

func (*ChannelState[T]) Send

func (cs *ChannelState[T]) Send(value T) bool

Send sends a value to the channel. It returns an error if the channel is closed. This method is thread-safe.

Example:

ch := chanplus.New[int](1)
err := ch.Send(42)
if err != nil {
    // Handle error (e.g., channel closed)
}

Jump to

Keyboard shortcuts

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