Skip to content

pixperk/stream-in-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

stream

A type-safe streaming library for Go with goroutine-safe sending and receiving.

Installation

go get github.com/pixperk/stream

Usage

package main

import "github.com/pixperk/stream/stream"

func main() {
    s := stream.NewStream[int](10)

    go func() {
        for i := 0; i < 50; i++ {
            s.Send(i)
        }
        s.Close()
    }()

    s.Consume(func(i int) error {
        // process data
        return nil
    })
}

API

NewStream[T any](buffer int) *Stream[T]

Creates a new stream with the specified buffer size.

Send(v T) error

Sends a value to the stream. Returns ErrStreamClosed if the stream is closed.

Recv() <-chan T

Returns a receive-only channel for reading values.

Err() <-chan error

Returns a receive-only channel for errors.

Consume(consumer func(T) error) error

Processes all values from the stream using the provided consumer function.

Close() error

Closes the stream gracefully.

Fail(err error)

Closes the stream with an error.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages