1.2 KiB
1.2 KiB
Go Type-Agnostic Collection Partitioning
Type-agnostic partitioning for anything that can be indexed in Go - slices, arrays,string
s. Inspired by Guava's Lists.partition
. This tiny library alleviates the issue of partitioning collections with wide ranging types - Go lacks generics - by returning consecutive index ranges that can be used on any indexable object.
Usage
...
// bigList can be any type
for idxRange := range gopart.Partition(len(bigList), partitionSize) {
bulkOperation(bigList[idxRange.Low:idxRange.High])
}
...
Installation
# install the library:
go get github.com/meirf/gopart
// use in your .go code:
import (
"github.com/meirf/gopart"
)
Implementation
The partitioning is done with a separate goroutine that passes the index ranges to a channel. This requires the use of a for...range loop, but adds concurrency and lowers memory usage (no slice of index ranges is stored anywhere).