第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

解組一個(gè)包含哈希表名稱的 toml

解組一個(gè)包含哈希表名稱的 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)好像當(dāng)我使用表格時(shí)[kiwi]似乎無(wú)法正確解組它。如果去掉表名,就可以成功抓取Id字段。在嘗試成功構(gòu)建將保存數(shù)據(jù)的整個(gè)結(jié)構(gòu)時(shí),我缺少一些封裝嗎?我嘗試了以下方法來(lái)添加表名,但沒(méi)有任何積極的結(jié)果:            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)但它的錯(cuò)誤是: o.Id undefined (type *fruitSpecs has no field or method Id)
查看完整描述

1 回答

?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

更新 1:我已經(jīng)設(shè)法用哈希表名稱對(duì)其進(jìn)行解碼。有關(guān)更多詳細(xì)信息,請(qǐng)參見(jiàn)以下示例:


            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

請(qǐng)注意三個(gè)變化,將標(biāo)簽添加到結(jié)構(gòu)中,o變量指向通用結(jié)構(gòu),并使用正確的路徑打印 id ( o.fruit.Id)


這里的問(wèn)題是我需要解析多個(gè)表,在標(biāo)簽中指定表名是不可行的。


有沒(méi)有辦法告訴 burntsushi toml parse 忽略表名并接受其中的所有內(nèi)容?就像是:


            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:最后我設(shè)法獲得了包含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請(qǐng)注意使用to的變化toml.Decode,將結(jié)構(gòu)生成到映射中fruitSpecs并在映射結(jié)構(gòu)上進(jìn)行交互。


這就是我解決這個(gè)問(wèn)題的方法。


免費(fèi)軟件。


查看完整回答
反對(duì) 回復(fù) 2023-02-28
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)