如何在Golang中正确地将切片映射分配给结构?我试着遵循,但它不起作用:
package main
import (
"fmt"
)
type Test struct {
name string
testCase map[string][]string
}
var t = Test{
name: "myTest",
testCase: map[string][]string{"key": {"value1", "value2"}}
}
func main() {
fmt.Println(t)
}
\main.go:14:61:语法错误:意外换行,应为逗号或}
分配值时,必须添加其类型作为前缀。
type Test struct {
name string
testCase map[string][]string
}
var t = Test{
name: "myTest",
testCase: map[string][]string{
"key": {"value1", "value2"},
},
}
不要忘记在项目末尾添加逗号分隔符,因为使用垂直样式的地图
参考