Skip to content

Instantly share code, notes, and snippets.

@fujimaki-k
Created October 30, 2025 03:48
Show Gist options
  • Select an option

  • Save fujimaki-k/ead92809ac577ff291b13551a405590b to your computer and use it in GitHub Desktop.

Select an option

Save fujimaki-k/ead92809ac577ff291b13551a405590b to your computer and use it in GitHub Desktop.
スライスを指定されたサイズのチャンクに分割します。
package main
import (
"fmt"
)
// chunks is divide slices into chunks of specified size.
func chunks[T any](slice []T, size int) [][]T {
var result [][]T
for i := 0; i < len(slice); i += size {
end := i + size
if end > len(slice) {
end = len(slice)
}
result = append(result, slice[i:end])
}
return result
}
func main() {
values := []string{"Honoka", "Kotori", "Umi", "Maki", "Rin", "Hanayo", "Nico", "Nozomi", "Eli"}
fmt.Println(chunks[string](values, 3))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment