Golang+gorm pagination, returning 50 pieces of data per page, is this correct?

the frontend is pulled down and refreshed, and the backend returns 50 pieces of data at a time. I wonder if the api, written by go+gorm is correct? Please have a look at
gorm: http://gorm.io/zh_CN/docs/que.

.
func ListArticle(offset int) ([]*ArticleModel, uint64, error) {
    articles := make([]*ArticleModel, 0)
    var count uint64

    if err := DB.Self.Where("status = ?", "1").Offset(offset).Limit(50).Order("id desc").Find(&articles).Count(&count).Error;  err != nil {
        return articles, count, err
    }

    return articles, count, nil
}

use offset+limit to implement this function, right? Is there anything that needs to be optimized?

Oct.29,2021

there's no problem, that's how it's written. Gorm's db is also similar to chained operations.


won't you report a "no rows in result set" error if you send a value greater than 1 to offset?

Menu