Skip to content

Instantly share code, notes, and snippets.

@gokhanm
Last active February 1, 2019 12:42
Show Gist options
  • Save gokhanm/1fd222e4cc5ded753a1eccdabb78aed4 to your computer and use it in GitHub Desktop.
Save gokhanm/1fd222e4cc5ded753a1eccdabb78aed4 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func chunkArray(array []string, limit int) [][]string {
var chunk []string
var chunks [][]string
for i:=0; i<len(array); i++ {
if len(chunk) == limit {
chunks = append(chunks, chunk)
chunk = nil
}
chunk = append(chunk, array[i])
}
chunks = append(chunks, chunk)
return chunks
}
func main() {
a := []string{"1","2","3","4","5","6","7","8","9","10"}
fmt.Println(chunkArray(a, 2))
//Output: [[1 2] [3 4] [5 6] [7 8] [9 10]]
// fmt.Println(chunkArray(a, 20))
//Output: [[1 2 3 4 5 6 7 8 9 10]]
// fmt.Println(chunkArray(a, 9))
//Output: [[1 2 3 4 5 6 7 8 9] [10]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment