Search code examples
arraysgoslicepartitioninggeneric-programming

In Go, how can I partition a slice/array/string of any type?


Guava has a generic partition method which can not directly be implemented in Go due to Go's lack of generics. Is there a workaround for this?


Solution

  • The gopart library addresses this. It allows partitioning of anything indexable in Go that has any type.

    for idxRange := range gopart.Partition(len(bigList), partitionSize) {
            bulkOperation(bigList[idxRange.Low:idxRange.High])
    }
    

    Full Executable Example