Package chunks allows to split a slice of any type into chunks with approximately equals sum of values.
go get github.com/go-deeper/chunks
package main
import (
"fmt"
"github.com/go-deeper/chunks"
)
func main() {
slice := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
sliceChunks := chunks.Split(slice, 9)
fmt.Println(sliceChunks)
}
Output:
[[1 2 3 4 5] [6 7 8 9 10]]
package main
import (
"fmt"
"github.com/go-deeper/chunks"
)
func main() {
slice := []int64{1, 2, 3, 4, 5, 6}
err := chunks.SplitFunc(slice, 5, func(chunk []int64) error {
fmt.Println(chunk)
return nil
})
fmt.Println(err)
}
Output:
[1 2 3]
[4 5 6]
<nil>
package main
import (
"errors"
"fmt"
"github.com/go-deeper/chunks"
)
func main() {
slice := make([]int64, 10)
err := chunks.SplitFunc(slice, 1, func(_ []int64) error {
return errors.New("some error")
})
fmt.Println(err)
}
Output:
some error
package main
import (
"fmt"
"github.com/go-deeper/chunks"
)
func main() {
slice := make([]string, 10)
err := chunks.SplitFunc(slice, 1, func(_ []string) error {
return chunks.ErrBreak
})
fmt.Println(err)
}
Output:
<nil>