美文网首页
数组分页

数组分页

作者: funcx | 来源:发表于2019-07-09 09:25 被阅读0次
    // 数组分页
    func PageData(xs interface{}, page, item int) (interface{}, int) {
        fv := reflect.ValueOf(xs)
        if fv.Kind() != reflect.Slice {
            panic("切片分页元数据必须为切片")
        }
        if page == 0 { //第0页返回所有
            return xs, fv.Len()
        }
        data := []interface{}{}
        for i := Skip(page, item); i < fv.Len() && i < Skip(page, item)+Limit(page, item); i++ {
            data = append(data, fv.Index(i).Interface())
        }
        return data, fv.Len()
    }
    

    相关文章

      网友评论

          本文标题:数组分页

          本文链接:https://www.haomeiwen.com/subject/xustkctx.html