gpt4 book ai didi

for-loop - 如何在Golang中的结构内迭代 slice ,还是可以在结构内使用for循环?

转载 作者:行者123 更新时间:2023-12-01 20:24:46 24 4
gpt4 key购买 nike

我有两个结构。一种用于彩票,一种用于奖励。我正在使用Mysql数据库。我想从数据库中读取数据并以JSON格式写入。我能够做到这一点,但是我想拥有一个嵌套结构,可以在Lottery结构中迭代Reward结构。我可以那样做吗?

这是我的彩票和奖励结构

type Lottery struct{
Id int `json:"lottery_id"`
Lottery string `json:"lottery_name"`
Description string `json:"lottery_description"`
Reward []Rew `json:"rewards"`
}

type Rew struct{
Id int `json:"reward_id"`
RewardName string `json:"reward_name"`
Description string `json:"reward_description"`
Asset int `json:"reward_asset"`
AssetName string `json:"reward_asset_name"`
}

这是我的代码
app.Get("/lottery/{id:int}", func (ctx iris.Context){
id1 := ctx.Params().GetIntDefault("id",0)

stmtOut, err := db.Prepare("select lottery_name, lottery_description from lottery_table where id = ?")
if err !=nil{
panic(err.Error())
}
defer stmtOut.Close()

var lottery_name, lottery_description string

err1 := stmtOut.QueryRow(id1).Scan(&lottery_name,&lottery_description)
if err != nil{
panic(err1.Error())
}

stmtOut1, err := db.Query("select id, reward_name, reward_description, reward_asset, reward_asset_name from rewards_table where lottery_id = ?",id1)
if err != nil{
panic(err.Error())
}
defer stmtOut1.Close()

for stmtOut1.Next() {

var id, reward_asset int
var reward_name, reward_description, reward_asset_name string

err2 := stmtOut1.Scan(&id, &reward_name, &reward_description, &reward_asset, &reward_asset_name)
if err2 != nil {
panic(err.Error())
}

rew := Lottery{
Id: id1,
Lottery: lottery_name,
Description: lottery_description,
Reward : []Rew{Rew{
Id: id,
RewardName: reward_name,
Description: reward_description,
Asset: reward_asset,
AssetName: reward_asset_name,
},
},
}

ctx.JSON(&rew)
}


})

当我使用上面的函数时,我正在得到这样的JSON格式
{
"lottery_id": 7,
"lottery_name": "lottery2",
"lottery_description": "lottery for 7",
"rewards": [
{
"reward_id": 9,
"reward_name": "Reward3",
"reward_description": "Reward for lottery 7",
"reward_asset": 20,
"reward_asset_name": "AC"
}
]
}{
"lottery_id": 7,
"lottery_name": "lottery2",
"lottery_description": "lottery for 7",
"rewards": [
{
"reward_id": 10,
"reward_name": "Reward5",
"reward_description": "Reward for lottery 7",
"reward_asset": 15,
"reward_asset_name": "AC"
}
]
}

但我想将JSON作为
{
"lottery_id":7,
"lottery_name":"lottery2"
"lottery_description":"lottery for 7"
"rewards":[
{
"reward_id":9,
"reward_name": "Reward3",
"reward_description": "Reward for lottery 7",
"reward_asset": 20,
"reward_asset_name": "ABC"
},
{ "reward_id":10,
"reward_name": "Reward5",
"reward_description": "Reward for lottery 7",
"reward_asset": 15,
"reward_asset_name": "ABC"
},
{ "reward_id":11,
"reward_name": "Reward7",
"reward_description": "Reward for lottery 7",
"reward_asset": 10,
"reward_asset_name": "ABC"
}
]
}

如何在struct中迭代?结构中是否可能有for循环?还是有其他方法可以获取上述JSON?

请帮我。

最佳答案

像这样:

app.Get("/lottery/{id:int}", func(ctx iris.Context) {
id1 := ctx.Params().GetIntDefault("id", 0)

stmtOut, err := db.Prepare("select lottery_name, lottery_description from lottery_table where id = ?")
if err != nil {
panic(err.Error())
}
defer stmtOut.Close()

lot := Lottery{Id: id1}
err1 := stmtOut.QueryRow(id1).Scan(&lot.Lottery, &lot.Description)
if err != nil {
panic(err1.Error())
}

stmtOut1, err := db.Query("select id, reward_name, reward_description, reward_asset, reward_asset_name from rewards_table where lottery_id = ?", id1)
if err != nil {
panic(err.Error())
}
defer stmtOut1.Close()

for stmtOut1.Next() {
rew := Rew{}
err2 := stmtOut1.Scan(&rew.Id, &rew.RewardName, &rew.Description, &rew.Asset, &rew.AssetName)
if err2 != nil {
panic(err.Error())
}
lot.Reward = append(lot.Reward, rew)

}
ctx.JSON(lot)
})

关于for-loop - 如何在Golang中的结构内迭代 slice ,还是可以在结构内使用for循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62400721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com