1

我从 Go 开始,对于已经了解 Go 并且只想查找资料的人来说,官方文档似乎更多。我希望在这里向正确的方向稍微推动一下。

我想要做什么:使用BurntSushi 的TOML 解析器解析一个TOML配置文件,该解析器由几个共享相同基本特征的元素组成。

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

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

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


演示.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"

演示.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")


}
4

1 回答 1

1

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

    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)
    }
于 2017-12-20T05:54:38.910 回答