gpt4 book ai didi

去嵌套结构

转载 作者:数据小太阳 更新时间:2023-10-29 03:23:36 26 4
gpt4 key购买 nike

我如何创建这样的 Go 结构,

{Name:Test { App1:Version1 App2:Version2}} 

App1/2Version1/2 是动态的地方使用 Go。

我试过以下方法,

type Final struct {
App string `json:"Name"`
Applications [] Application
}
type final []Final
type Application struct {
App string `json:`
}

final := Final {
Name:"Test",
Applications: []Application{
{GetApps()},
},
}

其中 GetApps 将返回应用程序详细信息列表,而我想要一个键值对,其中键是应用程序名称,值是其详细信息。我只需要一个语法,请帮忙

最佳答案

如前所述,为 FinalApplications 使用 map 可能是您所追求的。

type Final struct {
Name string `json:"Name"`
Applications map[string]string
}

然后您可以按照您的尝试分配它:

final := Final{
Name: "Test",
Applications: GetApps(),
}

确保 GetApps() 返回一个 map[string]string 并且在 json 编码时您将拥有动态键。

参见 an example here .

关于去嵌套结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48524298/

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