map[interface {}]interface {} yaml文件解码

map[interface {}]interface {}
 
map[interface{}]interface{} on a map[string]interface{} · Issue #139 · go-yaml/yaml https://github.com/go-yaml/yaml/issues/139
 
f1: hi世界
f2:
  f3: value
  f4: [42, 1024]
  f5: [a b]
  f6:
    f7: 77
    f8: ok

  

 
package main

import (
	"fmt"
	"io/ioutil"

	"gopkg.in/yaml.v3"
)

func main() {
	var err error
	m := make(map[string]interface{})
	var bs []byte
	bs, err = ioutil.ReadFile("a.yaml")
	if err != nil {
		panic(err)
	}
	err = yaml.Unmarshal(bs, &m)
	if err != nil {
		panic(err)
	}
	f1v := m["f1"].(string)
	var f3v string
	var f4v []int
	var f5v []string
	var f6v map[string]interface{}

	for k, v := range m["f2"].(map[string]interface{}) {
		switch k {
		case "f3":
			f3v = v.(string)
		case "f4", "f5":
			l := v.([]interface{})
			if k == "f4" {
				for _, u := range l {
					f4v = append(f4v, u.(int))
				}
			}
			if k == "f5" {
				for _, u := range l {
					f5v = append(f5v, u.(string))
				}
			}
		case "f6":
			f6v = v.(map[string]interface{})
		default:
		}
	}
	
	fmt.Println("---->", f1v, f3v, f4v, f5v, f6v)
}

  

go run *go
----> hi世界 value [42 1024] [a b] map[f7:77 f8:ok]

 
 
 
 
原文地址:https://www.cnblogs.com/rsapaper/p/15798414.html