range可以遍历切片,也可以遍历map
package main import ( "fmt" "sort" ) func main() { map2:= map[int]string{1:"王者农药",2:"绝地求生",3:"连连看",4:"传奇霸业",5:"消消乐"} fmt.Println(" map的长度:",len(map2)) //1.定义一个slice s1 := make([]int,0,len(map2)) //2.遍历map获取key-->s1中 for key := range map2{ s1 = append(s1, key) }