思想如下:利用map中key唯一的特性将slice中的数据保存到map的key中
但是要注意key的类型,有些数值不能做为key
Map 是一种无序的键值对的集合。Map 最重要的一点是通过 key 来快速检索数据,key 类似于索引,指向数据的值。
Map 是一种集合,所以我们可以像迭代数组和切片那样迭代它。不过,Map 是无序的,我们无法决定它的返回顺序,这是因为 Map 是使用 hash 表来实现的
Golang中map中key的类型
golang中的map,其中的 key 可以是很多种类型,比如 bool, 数字,string, 指针, channel , 还有只包含前面几个类型的 interface types, structs, arrays
显然,slice, map 还有 function 是不可以了,因为这几个没法用 == 来判断
原文如下:
As mentioned earlier, map keys may be of any type that is comparable. The language spec defines this precisely, but in short, comparable types are boolean, numeric, string, pointer, channel, and interface types, and structs or arrays that contain only those types. Notably absent from the list are slices, maps, and functions; these types cannot be compared using ==, and may not be used as map keys.
// 删除