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

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

如何使用 Viper 從嵌套的 YAML 結(jié)構(gòu)中獲取值?

如何使用 Viper 從嵌套的 YAML 結(jié)構(gòu)中獲取值?

Go
哈士奇WWW 2023-04-17 15:07:05
如何編寫下面的代碼以從我的嵌套 yaml 結(jié)構(gòu)中獲取字符串?這是我的 yaml:element:  - one:      url: http://test      nested: 123  - two:      url: http://test      nested: 123weather:  - test:      zipcode: 12345  - ca:      zipcode: 90210這是示例代碼viper.SetConfigName("main_config")  viper.AddConfigPath(".")  err := viper.ReadInConfig()  if err != nil {    panic(err)  }testvar := viper.GetString("element.one.url")我的問題:當我打印這個時,我得到一個空白字符串。根據(jù)文檔,這就是獲得嵌套元素的方式。我懷疑它不起作用,因為元素是列表。我需要做一個結(jié)構(gòu)嗎?我不確定如何制作,特別是如果它需要嵌套。
查看完整描述

3 回答

?
慕姐4208626

TA貢獻1852條經(jīng)驗 獲得超7個贊

您可以解組嵌套的配置文件。


主程序


package main


import (

    "fmt"

    "github.com/spf13/viper"

)


type NestedURL struct {

    URL string `mapstructure:"url"`

    Nested int `mapstructure:"nested"`

}


type ZipCode struct {

    Zipcode string `mapstructure:"zipcode"`

}


type Config struct {

    Element [] map[string]NestedURL `mapstructure:"element"`

    Weather [] map[string]ZipCode `mapstructure:"weather"`

}


func main() {

    viper.SetConfigName("config")

    viper.AddConfigPath(".")

    if err := viper.ReadInConfig();  err != nil {

        return

    }

    var config Config

    if err := viper.Unmarshal(&config); err != nil {

        fmt.Println(err)

        return

    }

    fmt.Println(config)

}

配置文件


element:

  - one:

      url: http://test

      nested: 123

  - two:

      url: http://test

      nested: 123

weather:

  - test:

      zipcode: 12345

  - ca:

      zipcode: 90210


查看完整回答
反對 回復 2023-04-17
?
臨摹微笑

TA貢獻1982條經(jīng)驗 獲得超2個贊

viper 庫中有不同的 Get 方法可用,并且您的 YML 結(jié)構(gòu)是類型[]map[string]string,因此要解析您的 YML 配置文件,您必須使用viper.Get方法。所以你必須像這樣解析你的文件..

注意:您也可以使用 struct 來取消編組數(shù)據(jù)。

package main


import (

? ? "fmt"


? ? "github.com/spf13/viper"

)


func main() {

? ? viper.SetConfigName("config")

? ? viper.AddConfigPath(".")

? ? err := viper.ReadInConfig()

? ? if err != nil {

? ? ? ? panic(err)

? ? }

? ? testvar := viper.Get("element")

? ? fmt.Println(testvar)

? ? elementsMap := testvar.([]interface{})

? ? for k, vmap := range elementsMap {

? ? ? ? fmt.Print("Key: ", k)?

? ? ? ? fmt.Println(" Value: ", vmap)

? ? ? ? eachElementsMap := vmap.(map[interface{}]interface{})


? ? ? ? for k, vEachValMap := range eachElementsMap {

? ? ? ? ? ? fmt.Printf("%v: %v \n", k, vEachValMap)

? ? ? ? ? ? vEachValDataMap := vEachValMap.(map[interface{}]interface{})


? ? ? ? ? ? for k, v := range vEachValDataMap {

? ? ? ? ? ? ? ? fmt.Printf("%v: %v \n", k, v)

? ? ? ? ? ? }

? ? ? ? }

? ? }

}


// Output:

/*

Key: 0 Value:? map[one:map[url:http://test nested:123]]

one: map[url:http://test nested:123]

url: http://test

nested: 123

Key: 1 Value:? map[two:map[url:http://test nested:123]]

two: map[url:http://test nested:123]

url: http://test

nested: 123

*/


查看完整回答
反對 回復 2023-04-17
?
江戶川亂折騰

TA貢獻1851條經(jīng)驗 獲得超5個贊

您可以使用Unmarshal或UnmarshalKey解析全部或部分數(shù)據(jù)并填充結(jié)構(gòu)。它與解組 json 非常相似。


在你的情況下,代碼將是這樣的:


package main


import (

    "fmt"

    "github.com/spf13/viper"

)


// here we define schema of data, just like what we might do when we parse json

type Element struct {

    Url    string `mapstructure:"url"`

    Nested int    `mapstructure:"nested"`

}


func main() {

    viper.SetConfigName("config")

    viper.AddConfigPath(".")

    err := viper.ReadInConfig()

    if err != nil {

        panic(err)

    }

    

    // data in `element` key is a map of string to Element. We define a variable to store data into it.

    elementParsed := make(map[string]*Element)

    // read the key `element` in the yaml file, and parse it's data and put it in `elementParsed` variable

    err = viper.UnmarshalKey("element", &elementParsed)

    if err != nil {

        panic(err)

    }


    fmt.Println(elementParsed["one"].Url) // will print: http://test 

    fmt.Println(elementParsed["one"].Nested) // will print: 123

}


查看完整回答
反對 回復 2023-04-17
  • 3 回答
  • 0 關(guān)注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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