pkg/slices¶
Generic helpers for common slice patterns—membership, counting, chunking, reversing, and deduplication—without re-writing loops at every callsite.
Quick Start¶
API Reference¶
| Function | Description |
|---|---|
EqualsIgnoreOrder(a, b) |
compare two slices while ignoring element order but respecting multiplicity |
Contains(slice, value) |
membership test |
ContainsCount(slice, value) |
count occurrences |
IndexOf(slice, value) |
index of first match (-1 if missing) |
IndexOfAll(slice, value) |
every index that matches |
Unique(slice) |
deduplicated copy preserving first-seen order |
Reverse(slice) |
in-place reversal |
Chunk(slice, size) |
split into evenly sized batches (last chunk may be smaller) |
All helpers are generic (Go 1.18+) and therefore type-safe across ints, structs, strings, etc. Functions that mutate (Reverse) do so in-place; the rest allocate new slices as needed.
Examples¶
EqualsIgnoreOrder¶
IndexOfAll¶
Chunk (Batching)¶
Chunkpanics whensize <= 0; validate user input before calling it.
Tips¶
- These helpers are a drop-in replacement for small anonymous loops, making tests and production code easier to read.
- Combine
Unique+Chunkto dedupe and batch IDs before fan-out. EqualsIgnoreOrderis helpful in tests for comparing slices where ordering is non-deterministic.