泛型参考文章:
https://blog.csdn.net/qq_4206...

实现批量切片功能, 类似 php array_chunk()函数
func ArrayChunk[T any](s []T, size int) [][]T {
    if size < 1 {
        panic("size: cannot be less than 1")
    }
    length := len(s)
    chunks := int(math.Ceil(float64(length) / float64(size)))
    var n [][]T
    for i, end := 0, 0; chunks > 0; chunks-- {
        end = (i + 1) * size
        if end > length {
            end = length
        }
        n = append(n, s[i*size:end])
        i++
    }
    return n
}

goper
413 声望26 粉丝

go 后端开发