在Ruby中,所谓的“哈希”在Go中称为“映射”(将键转换为值)。
但是,Go是静态类型检查的语言。映射只能将某种类型映射为另一种类型,例如map [string] int将字符串值映射为整数。那不是你想要的。
因此,您想要的是一个结构。实际上,您需要预先定义类型。所以你会怎么做:
// declaring a separate 'Date' type that you may or may not want to enpre as int. type Date int type User struct { Name string Dates []Date Images map[string]string}现在定义了该类型,您可以在另一种类型中使用它:
ar := []User{ User{ Name: "Tom", Dates: []Date{20170522, 20170622}, Images: map[string]string{"profile":"assets/tom-profile", "full": "assets/tom-full"}, }, User{ Name: "Pat", Dates: []Date{20170515, 20170520}, Images: map[string]string{"profile":"assets/pat-profile", "full": "assets/pat-full"}, },}请注意,我们如何将User定义为结构,将images定义为字符串到image的映射。您还可以定义单独的图像类型:
type Image struct { Type string // e.g. "profile" Path string // e.g. "assets/tom-profile"}然后,您将不会将Images定义为,
map[string]string而是定义为
[]ImageImage结构的切片。哪一种更合适取决于使用情况。



