最佳答案
我尝试生成一个 map,然后将其转换为 yaml 文件,如下所示:
uid :
kasi:
cn: Chaithra
street: fkmp
nandan:
cn: Chaithra
street: fkmp
remya:
cn: Chaithra
street: fkmp
我想我在创建地图时遗漏了一些重要的东西。我的代码如下。
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type T struct {
cn string
street string
}
func main() {
names := []string{"kasi", "remya", "nandan"}
m := make(map[string]map[string]T, len(names))
for _, name := range names {
//t := T{cn: "Chaithra", street: "fkmp"}
m["uid"][name] = T{cn: "Chaithra", street: "fkmp"}
}
fmt.Println(m)
y, _ := yaml.Marshal(&m)
fmt.Println(string(y))
//fmt.Println(m, names)
}
它出现了以下错误:
panic: runtime error: assignment to entry in nil map