登录
首页 >  Golang >  Go问答

解析包含哈希表名称的 Toml 文件

来源:stackoverflow

时间:2024-02-07 20:42:22 187浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《解析包含哈希表名称的 Toml 文件》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

使用 go 的 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未定义(类型*fruitspecs没有字段或方法id)


正确答案


更新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
// 9876544312

注意使用 toml.unmarshalltoml.decode 的变化,将 fruitspecs 的结构生成到映射中,并在映射的结构上进行交互。

这就是我解决这个问题的方法。

免费软件。

终于介绍完啦!小伙伴们,这篇关于《解析包含哈希表名称的 Toml 文件》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>