batchan package - github.com/floatdrop/batchan - Go Packages

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

This section is empty.

This section is empty.

func New[T any](in <-chan T, size int, opts ...option[T]) <-chan []T
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[T any](splitFunc func(T, T) bool) option[T]

This section is empty.