gpt4 book ai didi

go - 如何解析 TOML 中的嵌套数组/子表?

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

我从 Go 和官方 documentation 开始对于已经了解 Go 并且只想查找资料的人来说似乎更多。我希望在这里朝着正确的方向稍微插入一下。

我想做的事:使用 BurntSushi 的 TOML 解析器解析 TOML 配置文件,该解析器由共享相同基础的多个元素组成特征。

卡住的地方:我希望每个项目的各个部分都与项目一起列出。到目前为止,我只能通过其索引获得其中之一。我正在寻找的是如何以列出相应索引的所有子部分而不是仅列出特定索引的方式进行设置。我可以使用 [:] 获取 JSON 列表,但似乎无法对其进行调整以获取正常输出。

最初我考虑过 [[item.part.001]] 等等,因为它在在线 JSON 解析器中看起来是正确的,但无法弄清楚如何正确地将其读入走。由于无论如何我都被困住了,我对这两种类型都持开放态度,无论哪种方式效果最好。

提前致谢。以下是作为简化的最小工作示例的文件。


demo.toml

# — — — — —  — — — — — — — — — — — — — — — — — — 
# First Item
# — — — — — — — — — — — — — — — — — — — — — — —

[[item]]
itemname = "Fragments"
itemdesc = "This one can get a bit longer."

[item.attributes]
material = "Basematname"
light = "Lightname"
transpc = "full"
displace = "height"

[[item.part]]
partname = "Shard"
partlink = "active"

[[item.part]]
partname = "Tear"
partlink = "deferred"

[[item.part]]
partname = "crater"
partlink = "disabled"


# — — — — — — — — — — — — — — — — — — — — — — —
# Second Item
# — — — — — — — — — — — — — — — — — — — — — — —

[[item]]
itemname = "Splash"
itemdesc = "This one also can get a bit longer."

[item.attributes]
material = "Other Basematname"
light = "Other Lightname"
transpc = "half"
displace = "bump"

[[item.part]]
partname = "Drops"
partlink = "active"

[[item.part]]
partname = "Wave"
partlink = "deferred"

demo.go

package main 

import (
"fmt"
"github.com/BurntSushi/toml"
)

type item struct {
ItemName string
ItemDesc string
Attributes attributes
Part []part
}

type part struct {
PartName string
PartLink string
}

type attributes struct {
Material string
Light string
TransPC string
Displace string
}

type items struct {
Item []item
}



func main() {


var allitems items

if _, err := toml.DecodeFile("demo.toml", &allitems); err != nil {
fmt.Println(err)
return
}

fmt.Printf("\n")

for _, items := range allitems.Item {
fmt.Printf(" Item Name: %s \n", items.ItemName)
fmt.Printf(" Description: %s \n\n", items.ItemDesc)

fmt.Printf(" Material: %s \n", items.Attributes.Material)
fmt.Printf(" Lightmap: %s \n", items.Attributes.Light)
fmt.Printf(" TL Precision: %s \n", items.Attributes.TransPC)
fmt.Printf(" DP Channel: %s \n", items.Attributes.Displace)


fmt.Printf(" Part Name: %s \n", items.Part[0].PartName)
fmt.Printf(" Part Link: %s \n", items.Part[0].PartLink)
# ^
# That's where [:] won't do it.

fmt.Printf("\n────────────────────────────────────────────────────┤\n\n")


}

fmt.Printf("\n")


}

最佳答案

正如评论中所指出的,您需要一个嵌套循环。而不是这个:

    fmt.Printf("    Part Name: %s \n", items.Part[0].PartName)
fmt.Printf(" Part Link: %s \n", items.Part[0].PartLink)

使用这个:

    for _, part := range items.Part {
fmt.Printf(" Part Name: %s \n", part.PartName)
fmt.Printf(" Part Link: %s \n", part.PartLink)
}

关于go - 如何解析 TOML 中的嵌套数组/子表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47860883/

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