我正在尋找未結婚的簡單yaml,但有些事情不對勁。已經花了足夠的時間。請幫忙嗎?package mainimport ( "fmt" yaml "gopkg.in/yaml.v2")func main() { raw := `targets: - from: "http://localhost:8080/test1" timeout: "10s" - from: "http://localhost:8080/test2" timeout: "30s"` type Target struct { from string `yaml:"from"` timeout string `yaml:"timeout"` } type config struct { Targets []Target `yaml:"targets"` } cfg := config{} err := yaml.Unmarshal([]byte(raw), &cfg) if err != nil { fmt.Println(err) } fmt.Println("Config", cfg)}我低于空的 o/pConfig {[{ } { }]}兒童游樂場 -> https://play.golang.org/p/LANMpq_zPP9
1 回答

森欄
TA貢獻1810條經驗 獲得超5個贊
您必須導出結構中的字段。如 api 文檔中所述:
僅當導出結構字段(具有大寫首字母)時,結構字段才會取消編組,并且使用小寫的字段名稱作為默認鍵來取消編組。
(https://github.com/go-yaml/yaml/blob/496545a6307b/yaml.go#L88)
將 -struct 更改為:Target
type Target struct {
From string `yaml:"from"`
Timeout string `yaml:"timeout"`
}
應該工作。
試試看:https://play.golang.org/p/ZD7Jrv0QBdn
- 1 回答
- 0 關注
- 126 瀏覽
添加回答
舉報
0/150
提交
取消