drrvnbwle80177811 2017-12-18 00:53
浏览 927
已采纳

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

I'm starting with Go and the official documentation seems more for people who know Go already and just want to look stuff up. I'm hoping for a little nudge in the right direction here.

What I'm looking to do: Parse a TOML config file with BurntSushi's TOML parser that consists of several elements sharing the same base characteristics.

Where I'm stuck: I want each item's respective parts to be listed with the item. So far I can only get one of them by its index. What I'm looking for is how to set it up in a way that lists all sub parts of the respective index instead of just a specific one. I can get a JSON list with [:] but can't seem to adapt that to get normal output.

Initially I had considered [[item.part.001]] and so on because it looked right in the online JSON parsers, but wasn't able to figure out how to read that properly into Go. Since I'm stuck anyway, I'm open to both types, whatever works best.

Thanks in advance. Here are the files as an abbreviated minimum working example.


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("
")

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

", items.ItemDesc)

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


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

        fmt.Printf("
────────────────────────────────────────────────────┤

")


    }

    fmt.Printf("
")


}
  • 写回答

1条回答 默认 最新

  • douju2599 2017-12-20 05:54
    关注

    As pointed out in the comments, you need a nested loop. Instead of this:

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

    use this:

        for _, part := range items.Part {
            fmt.Printf("    Part Name: %s 
    ", part.PartName)
            fmt.Printf("    Part Link: %s 
    ", part.PartLink)
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件