Skip to content

Instantly share code, notes, and snippets.

@ak-ymst
Created March 28, 2019 08:14
Show Gist options
  • Save ak-ymst/4a5c2b35e129e3819861b0190beeec28 to your computer and use it in GitHub Desktop.
Save ak-ymst/4a5c2b35e129e3819861b0190beeec28 to your computer and use it in GitHub Desktop.
func ChunkingSlice(list interface{}, size int) []interface{} {
if size <= 0 {
return []interface{}{}
}
// sliceかどうかのチェック
if reflect.TypeOf(list).Kind() != reflect.Slice {
return []interface{}{}
}
var result = []interface{}{}
var v = reflect.ValueOf(list)
for i := 0; i < v.Len(); i += size {
if i+size <= v.Len() {
result = append(result, v.Slice(i, i+size).Interface())
} else {
result = append(result, v.Slice(i, v.Len()).Interface())
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment