Last active
February 8, 2019 17:45
-
-
Save betandr/48c4712297678fb1baffe3b79e4a6147 to your computer and use it in GitHub Desktop.
Split a 1D slice into 2D slice (triples)
This file contains 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 main() { | |
s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} | |
fmt.Println(split(s, 3)) | |
} | |
func split(s []int, width int) [][]int { | |
ss := make([][]int, len(s)/width) | |
for si, wi := 0, 0; si < len(s); si, wi = si+3, wi+1 { | |
ss[wi] = s[si:(si + width)] | |
} | |
return ss | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment