Skip to content

Instantly share code, notes, and snippets.

@betandr
Last active February 8, 2019 17:45
Show Gist options
  • Save betandr/48c4712297678fb1baffe3b79e4a6147 to your computer and use it in GitHub Desktop.
Save betandr/48c4712297678fb1baffe3b79e4a6147 to your computer and use it in GitHub Desktop.
Split a 1D slice into 2D slice (triples)
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