Golang中无法返回对象数组而是返回单个对象的问题
Golang中无法返回对象数组而是返回单个对象的问题 //查找所有钱包历史记录 - 模型函数
func FindAllHistory(condition interface{}) (WalletTransactionModel, error) {
db := common.GetDB()
var model WalletTransactionModel
err := db.Where(condition).Find(&model).Error
return model, err
}
7 回复
感谢 @kync。代码可以执行,但我仍然只得到一个对象实例,而不是终端中显示的两个实例。
更多关于Golang中无法返回对象数组而是返回单个对象的问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
您应该向 Find 方法传递一个数组指针,而不是单个对象。
var model WalletTransactionModel
datas := []WalletTransactionModel{}
err := db.Where(condition).Find(&datas).Error
欢迎来到 Go 论坛 🚀🎆。
那么你的问题是?
请注意:
- 在不了解其结构描述的情况下,我们无法确定
WalletTransactionModel。 common是哪个数据库包?
这是我的模型文件中的一个函数,它应该根据条件收集所有记录。举个例子,我执行查询,它返回单个对象,但我的终端显示至少有5行数据被影响。所以我的困难在于,我似乎不知道如何将对象数组传递给调用函数进行显示。问题是它获取了多条记录,但只返回一条。
type WalletTransactionModel struct {
ID int `gorm:"primary_key;AUTO_INCREMENT"`
Category string `gorm:"not null"`
Sender string `gorm:"not null"`
Receiver string `gorm:"not null"`
Description string `gorm:"not null"`
SessionID string `gorm:"-"`
PIN string `gorm:"-"`
OriginatingTransaction int
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
这是我的
我似乎不知道如何将对象数组发送到调用函数进行显示。
我们需要了解更多关于 FindAllHistory(...) 的信息,它有文档吗(函数顶部的那些注释)?
type WalletTransactionModel struct { ID int `gorm:"primary_key;AUTO_INCREMENT"` TransactionAmount float64 `gorm:"type:decimal(10,2);not null"` // Transaction amount TransactionFees float64 `gorm:"type:decimal(10,2);not null"` // Transaction fee TotalAmount float64 `gorm:"type:decimal(10,2);not null"` // Transaction total Direction string `gorm:"not null"` // credit or Debit Category string `gorm:"not null"` Sender string `gorm:"not null"` Receiver string `gorm:"not null"` Description string `gorm:"not null"` OldPendingBalance float64 `gorm:"type:decimal(10,2);not null"` NewPendingBalance float64 `gorm:"type:decimal(10,2);not null"` OldAvailableBalance float64 `gorm:"type:decimal(10,2);not null"` NewAvailableBalance float64 `gorm:"type:decimal(10,2);not null"` CardReference string `gorm:"not null;unique_index:idx_ajocard_reference"` // DestinationBankAccount string DestinationBankCode string SessionID string `gorm:"-"` PIN string `gorm:"-"` OriginatingTransaction int CreatedAt time.Time UpdatedAt time.Time DeletedAt *time.Time }
提醒一句:由于这涉及财务数据(无论是真实货币还是并发操作),请务必小心,并花时间过滤掉敏感信息。
在Golang中,您遇到的问题是因为使用了Find(&model)但model是单个结构体实例,而不是切片。Find()方法期望接收切片或数组来存储多个结果。
以下是修正后的代码:
// 查找所有钱包历史记录 - 模型函数
func FindAllHistory(condition interface{}) ([]WalletTransactionModel, error) {
db := common.GetDB()
var models []WalletTransactionModel
err := db.Where(condition).Find(&models).Error
return models, err
}
关键修改:
- 将返回类型从
WalletTransactionModel改为[]WalletTransactionModel - 将变量
model改为models并声明为切片:[]WalletTransactionModel Find(&models)现在会正确填充所有匹配的记录
如果需要查询单条记录,应该使用First()或Take()方法:
// 查找单条钱包历史记录
func FindOneHistory(condition interface{}) (WalletTransactionModel, error) {
db := common.GetDB()
var model WalletTransactionModel
err := db.Where(condition).First(&model).Error
return model, err
}
如果使用GORM的预加载关联数据:
func FindAllHistoryWithRelations(condition interface{}) ([]WalletTransactionModel, error) {
db := common.GetDB()
var models []WalletTransactionModel
err := db.Where(condition).
Preload("User").
Preload("Wallet").
Find(&models).Error
return models, err
}
分页查询示例:
func FindHistoryWithPagination(condition interface{}, page, pageSize int) ([]WalletTransactionModel, int64, error) {
db := common.GetDB()
var models []WalletTransactionModel
var total int64
// 获取总数
db.Model(&WalletTransactionModel{}).Where(condition).Count(&total)
// 分页查询
offset := (page - 1) * pageSize
err := db.Where(condition).
Offset(offset).
Limit(pageSize).
Find(&models).Error
return models, total, err
}

