我正在使用 Postgresql 数据库。这是我要返回多行的代码块:
type NewsPaper struct {
language string
logo_url string
slug string
ranking string
title string
search_term string
}
func GetNewsPapers() []byte{
db := GetDB()
newspaper := NewsPaper{}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
rows, err := db.Query(`SELECT title, language, ranking, slug, search_term, logo_url FROM public.news_newspaper`)
if err != nil {
log.Println(err)
}
defer rows.Close()
//results := []NewsPaper // error : type []NewsPaper is not an expression
for rows.Next() {
err = rows.Scan(&newspaper.title, &newspaper.language, &newspaper.ranking, &newspaper.slug, &newspaper.search_term, &newspaper.logo_url)
if err != nil {
log.Println(err)
}
//results = append(results, newspaper)
}
fmt.Println(newspaper) // printing last row
err = rows.Err()
if err != nil {
panic(err)
}
values, _ := json.Marshal(newspaper)
//fmt.Println(values)
return values
}
问题是它从表中返回最后一行。我也尝试将结果存储在数组中,但我没有成功。
元芳怎么了
HUWWW
largeQ
随时随地看视频慕课网APP
相关分类