尽管在初始化复合文字时需要“提及”您的类型,但您不需要事先声明变量类型,至少不要在此简单示例中声明变量类型。例如[{}]
(对象数组?)是没有意义的围棋编译器,而不是你需要写这样的事情[]map[string]interface{}{}
(地图切片,它的键是字符串,其值可以是任何类型)
进行分解:
[]
- 无论何种类型片来后
map
- 内置的地图(哈希认为)
[string]
- 方括号中是地图密钥类型, 可以几乎任何类型的
interface{}
- 地图的类型值
{}
- 这个初始化/分配整个事情
所以在进入你的例子会是这个样子:
var Array = []map[string]interface{}{
{"name":"Tom", "dates": []int{20170522, 20170622}, "images": map[string]string{"profile": "assets/tom-profile", "full": "assets/tom-full"}},
{"name":"Pat", "dates": []int{20170515, 20170520}, "images": map[string]string{"profile": "assets/pat-profile", "full": "assets/pat-full"}},
// ...
}
阅读更多地图和你可以在这里使用的关键类型:https://golang.org/ref/spec#Map_types
这就是说,在Go中,大部分时间,你会首先更具体地定义你的结构化类型,然后用它们代替地图,所以像这样的东西在Go中更有意义:
type User struct {
Name string
Dates []int
Images Images
}
type Images struct {
Profile string
Full string
}
var Array = []User{
{Name:"Tom", Dates:[]int{20170522, 20170622}, Images:Images{Profile:"assets/tom-profile", Full:"assets/tom-full"}},
{Name:"Pat", Dates:[]int{20170515, 20170520}, Images:Images{Profile:"assets/pat-profile", Full:"assets/pat-full"}},
}
'type Row struct {name string ....}'--- start带着某些东西,并在有特定问题时返回。 – zerkms