Last active
February 1, 2019 12:42
-
-
Save gokhanm/1fd222e4cc5ded753a1eccdabb78aed4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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