batchan

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 1 Imported by: 0

README

BatChan

CI Go Report Card Go Reference

A lightweight Go library for batching values from a channel with support for size-based and optional timeout-based flushing.

Features

  • Batch items from a channel based on size
  • Flush batches after a timeout, even if not full
  • Zero-dependency, idiomatic Go

Installation

go get github.com/floatdrop/batchan

Usage

package main

import (
	"fmt"
	"time"

	"github.com/floatdrop/batchan"
)

func main() {
	input := make(chan string, 5)
	batches := batchan.New(input, 3)

	go func() {
		inputs := []string{"A", "B", "C", "D", "E"}
		for _, v := range inputs {
			input <- v
		}
		close(input)
	}()

	for v := range batches {
		fmt.Println("Got:", v)
	}

	// Output:
	// Got: [A B C]
	// Got: [D E]
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New[T any](in <-chan T, size int, opts ...option[T]) <-chan []T
Example
package main

import (
	"fmt"

	"github.com/floatdrop/batchan"
)

func main() {
	input := make(chan string, 5)
	batches := batchan.New(input, 3)

	go func() {
		inputs := []string{"A", "B", "C", "D", "E"}
		for _, v := range inputs {
			input <- v
		}
		close(input)
	}()

	for v := range batches {
		fmt.Println("Got:", v)
	}

}
Output:

Got: [A B C]
Got: [D E]

func WithSplitFunc added in v0.0.2

func WithSplitFunc[T any](splitFunc func(T, T) bool) option[T]

func WithTimeout

func WithTimeout[T any](timeout time.Duration) option[T]

Types

This section is empty.

Jump to

Keyboard shortcuts

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