Created
October 30, 2025 03:48
-
-
Save fujimaki-k/ead92809ac577ff291b13551a405590b 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" | |
| ) | |
| // 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