我正在尝试生成地图,然后将其转换为如下所示的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
1
运行时错误的
—
Flimzy 2015年