我正在尝试从表中获取所有数据。它返回所有数据,但仅显示最后一个记录。我正在使用Gorm和Gin和Golang。
我尝试创建一个结构并将该结构传递给Find
方法。
type MpCountry struct{
id uint
Iso string
Name string
Nicename string
Iso3 string
Numcode uint
phonecode uint
}
代码:
countries:= DbModel.MpCountry{}
DbModel.DB.Debug().Find(&countries)
fmt.Println(countries)
fmt.Println("test")
log.Printf("%+v", countries)
return &countries
输出
SELECT * FROM `mp_countries`
[239 rows affected or returned ]
{id:0 Iso:ZW Name:ZIMBABWE Nicename:Zimbabwe Iso3:ZWE Numcode:716 phonecode:0}
您仅在结构中传递,而不是结构的切片。因此,它替换了结构中的值,直到达到最后一个记录。
。相反,将指针传递给切片:
countries := &[]DbModel.MpCountry{}
DbModel.DB.Debug().Find(countries)