栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Golang中制作哈希数组

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Golang中制作哈希数组

在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
而是定义为
[]Image
Image结构的切片。哪一种更合适取决于使用情况。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392341.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号