duanlaiquan8174
2019-05-15 00:31
浏览 11
已采纳

如何使用struct显示所有记录

I am trying to fetch all data from a table. It returns all data but it is displaying only the last record. I am using GORM and GIN with Golang.

I tried to create a struct and pass that struct to the Find method.

type MpCountry struct{
        id uint
        Iso string
        Name string
        Nicename string
        Iso3 string
        Numcode uint
        phonecode uint
}

Code:

countries:=  DbModel.MpCountry{}
DbModel.DB.Debug().Find(&countries)
fmt.Println(countries)
fmt.Println("test")
log.Printf("%+v", countries)
return  &countries

Output

SELECT * FROM `mp_countries`
[239 rows affected or returned ]

{id:0 Iso:ZW Name:ZIMBABWE Nicename:Zimbabwe Iso3:ZWE Numcode:716 phonecode:0}

1条回答 默认 最新

相关推荐 更多相似问题