使用gorm Golang lib构建自定义查询

I'm using http://jinzhu.me/gorm/crud.html#query

I want to build selectQuery base on some custom conditions

selectQuery := db.Select("username").Where("status = 'active'")
selectQuery.Limit(10)
if err := selectQuery.Find(&users).Error; err != nil {
//
} else {
//
}

Why limit is not applied?

It’s working if implemented with below code:

if err := db.Select("username").Where("status = 'active").Limit(10).Find(&users).Error; err != nil {
//
} else {
//
}

Why limit is not applied?

Because you're ignoring the return value of Limit. In effect, you're creating a query with a limit, then throwing it away without ever executing it.

You need to use:

selectQuery = selectQuery.Limit(10)