PageToSlice

master
nikkiing 2 years ago
parent c8885264bf
commit d651c6fb09
  1. 30
      pagination/v1/pagination.go

@ -21,6 +21,36 @@ type Integer interface {
~int | ~int32
}
// 用于对一个切片分页
func PageToSlice[T ~int](page, pageSize, total T) (begin, end T) {
if page < DefaultPage {
page = DefaultPage
}
if pageSize <= MinPageSize {
pageSize = MinPageSize
}
if pageSize >= MaxPageSize {
pageSize = MaxPageSize
}
// 计算总页数
totalPage := (total / pageSize) + 1
if page > totalPage {
page = totalPage
}
begin = (page - 1) * pageSize
if total-begin <= pageSize {
end = total
} else {
end = begin + pageSize
}
return
}
// 用于分页查询
func PageToOffset[T Integer](page, pageSize T) (offset, limit T) {
if page < DefaultPage {
page = DefaultPage

Loading…
Cancel
Save