计数 returns 个错误结果
Count returns wrong result
我有一个用户模型,目前只有一行。我正在尝试计算整个用户 table 的行数,这是我的代码:
var count int64
db.Model(&models.User{}).Count(count)
fmt.Println(count)
我期待 1 但它正在打印 0。
使用 gorm 在 table 中打印行数的正确方法是什么?
更新:我的用户模型:
package models
import "github.com/jinzhu/gorm"
type User struct {
gorm.Model
Name string
Password string
Admin bool
}
您需要将 count
变量作为引用传递给 Counter
,因为它现在是按值传递的。你应该这样做:
db.Model(&models.User{}).Count(&count)
我有一个用户模型,目前只有一行。我正在尝试计算整个用户 table 的行数,这是我的代码:
var count int64
db.Model(&models.User{}).Count(count)
fmt.Println(count)
我期待 1 但它正在打印 0。 使用 gorm 在 table 中打印行数的正确方法是什么?
更新:我的用户模型:
package models
import "github.com/jinzhu/gorm"
type User struct {
gorm.Model
Name string
Password string
Admin bool
}
您需要将 count
变量作为引用传递给 Counter
,因为它现在是按值传递的。你应该这样做:
db.Model(&models.User{}).Count(&count)