Split slice in chunks

2019-11-01

After benchmarking other solutions, this came out to be the most efficient.

Go Playground

 1func split(buf []string, lim int) [][]string {
 2	var chunk []string
 3	chunks := make([][]string, 0, len(buf)/lim+1)
 4	for len(buf) >= lim {
 5		chunk, buf = buf[:lim], buf[lim:]
 6		chunks = append(chunks, chunk)
 7	}
 8	if len(buf) > 0 {
 9		chunks = append(chunks, buf[:len(buf)])
10	}
11	return chunks
12}