collection

module
v0.0.0-...-9bb3c31 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT

README

Collection

Go Reference Go Version CI codecov Go Report Card

A powerful Go port of Laravel Collections — fluent, type-safe, and powered by Go generics and iter.Seq lazy evaluation.

go get github.com/gocanto/collection

⚡️ Why use this?

Go's slices and maps are powerful but often lead to repetitive boilerplate loops for common operations. collection provides a fluent, expressive API to handle data transformations with ease:

  • 💎 Fluent Pipelines: Chain .Filter().Take().Each() instead of nesting for loops.
  • 🛡️ Type-Safe: Built with Go generics—no interface{} casting or runtime type errors.
  • ♻️ Immutable by Default: Transformation methods return new collections, preserving your original data.
  • 🐢 Lazy Evaluation: Process massive datasets efficiently using iter.Seq sequences.
  • 🧩 Modular: Use the full fluent API or lightweight standalone utilities (arr and kv).

🚀 Quick Start

package main

import (
    "fmt"
    "github.com/gocanto/collection/collection"
)

func main() {
    // 1. Create a collection from a slice
    numbers := collection.Collect([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})

    // 2. Chain operations fluently
    even := numbers.
        Filter(func(n int, _ int) bool { return n % 2 == 0 }).
        Take(3)

    // 3. Transform types using top-level generics
    // (Go doesn't allow methods to introduce new type parameters)
    result := collection.Map(even, func(n int, _ int) string {
        return fmt.Sprintf("Number: %d", n)
    })

    fmt.Println(result.All()) // ["Number: 2", "Number: 4", "Number: 6"]
}

📦 Package Ecosystem

Package Purpose Use when...
collection The Core You want the full fluent Collection[T] experience for slices.
lazy Lazy Sequences You're handling large/infinite streams and want deferred execution.
collectible Key-Value Maps You need an ordered collectible.Collection[K, V] with a fluent API.
arr Slice Utils You need a quick one-off helper (e.g., Sort, Flatten) on a raw []T.
kv Map Utils You need dot-notation access ("user.profile.name") for map[string]any.

📖 Deep Dive

Explore our comprehensive documentation for each component:


⚖️ License

Released under the MIT License.

Directories

Path Synopsis
Package arr provides generic utility functions for working with slices.
Package arr provides generic utility functions for working with slices.
Package collectible provides an ordered map collection with a fluent, chainable API.
Package collectible provides an ordered map collection with a fluent, chainable API.
Package collection provides a fluent, generic wrapper for working with slices of data.
Package collection provides a fluent, generic wrapper for working with slices of data.
Package kv provides utility functions for working with maps.
Package kv provides utility functions for working with maps.
Package lazy provides lazily evaluated generic sequences backed by iter.Seq.
Package lazy provides lazily evaluated generic sequences backed by iter.Seq.
Package support provides shared types and error definitions used across the collection family of packages.
Package support provides shared types and error definitions used across the collection family of packages.

Jump to

Keyboard shortcuts

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