解组一个包含哈希表名称的 toml

使用 toml 解析器 ( https://github.com/BurntSushi/toml )


我正在尝试解组以下 toml 文件:


            type (

                fruitSpecs struct {

                    Id     int      `toml:"id"`

                    Name   string   `toml:"name"`

                }

            )

            blob := `

            [kiwi]

                id = 1234581941

                name = "kiwi"

            `

            o := &fruitSpecs{}

            err := toml.Unmarshal([]byte(blob), o)

            fmt.Println(o.Id)


好像当我使用表格时[kiwi]似乎无法正确解组它。


如果去掉表名,就可以成功抓取Id字段。


在尝试成功构建将保存数据的整个结构时,我缺少一些封装吗?


我尝试了以下方法来添加表名,但没有任何积极的结果:


            type (

                fruitSpecs struct {

                    Id     int      `toml:"id"`

                    Name   string   `toml:"name"`

                }

                fruits struct {

                    fruit fruitSpecs

                }

            )

            blob := `

            [kiwi]

                id = 1234581941

                name = "kiwi"

            `

            o := &fruitSpecs{}

            err := toml.Unmarshal([]byte(blob), o)

            fmt.Println(o.Id)


但它的错误是: o.Id undefined (type *fruitSpecs has no field or method Id)


慕村225694
浏览 112回答 1
1回答

慕工程0101907

更新 1:我已经设法用哈希表名称对其进行解码。有关更多详细信息,请参见以下示例:            type (                fruitSpecs struct {                    Id     int      `toml:"id"`                    Name   string   `toml:"name"`                }                fruits struct {                    fruit fruitSpecs `toml:"kiwi"`                }            )            blob := `            [kiwi]                id = 1234581941                name = "kiwi"            `            o := &fruits{}            err := toml.Unmarshal([]byte(blob), o)            fmt.Println(o.fruit.Id)// CLI Output:// 1234581941请注意三个变化,将标签添加到结构中,o变量指向通用结构,并使用正确的路径打印 id ( o.fruit.Id)这里的问题是我需要解析多个表,在标签中指定表名是不可行的。有没有办法告诉 burntsushi toml parse 忽略表名并接受其中的所有内容?就像是:            type (                fruitSpecs struct {                    Id     int      `toml:"id"`                    Name   string   `toml:"name"`                }                fruits struct {                    fruit fruitSpecs `toml:"*"` // Do not filter by name, accept every table name entry                }            )            blob := `            [kiwi]                id = 1234581941                name = "kiwi"            [banana]                id = 9876544312                name = "banana"            `            o := &fruits{}            err := toml.Unmarshal([]byte(blob), o)            fmt.Println(o.fruit.Id)// Desired output:// 1234581941// 9876544312更新 2:最后我设法获得了包含Id以下代码的所有字段:            type (                fruitSpecs struct {                    Id     int      `toml:"id"`                    Name   string   `toml:"name"`                }                fruit map[inteface{}]fruitSpecs            )            blob := `            [kiwi]                id = 1234581941                name = "kiwi"            [banana]                id = 9876544312                name = "banana"            `            var o fruit            err := toml.Decode(blob, &fruit)            for _, item := range o {                fmt.Println(item.Id)            }// CLI Output:// 1234581941// 9876544312toml.Unmarshall请注意使用to的变化toml.Decode,将结构生成到映射中fruitSpecs并在映射结构上进行交互。这就是我解决这个问题的方法。免费软件。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go