亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

解組一個包含哈希表名稱的 toml

解組一個包含哈希表名稱的 toml

Go
慕村225694 2023-02-28 21:18:28
使用 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)
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

更新 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.Unmarshall請注意使用to的變化toml.Decode,將結構生成到映射中fruitSpecs并在映射結構上進行交互。


這就是我解決這個問題的方法。


免費軟件。


查看完整回答
反對 回復 2023-02-28
  • 1 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號