其他分享
首页 > 其他分享> > go map的三种定义方式

go map的三种定义方式

作者:互联网

package main
import "fmt"
func main() {   /定义map变量   var a map[int]string   //只声明map内存是没有分配空间   //必须通过make函数进行初始化,才会分配空间   a = make(map[int]string, 10) //map可以存放10个键值对   //将键值对存入map中   a[20190120] = "陈中头"   a[20190121] = "陈大头"   a[20190122] = "陈小偷"
  fmt.Println(a)
  for _, v := range a {     fmt.Println(v)   }
  //方式1   var b map[int]string
  b = make(map[int]string, 10)   b[20190902] = "张三"   b[20190903] = "李四"
  fmt.Println(b)
  //方式2   c := make(map[int]string)   c[20190902] = "张三"   c[20190903] = "李四"   fmt.Println(c)
  //方式3   d := map[int]string{     20190902: "张三",     20190903: "李四",   }
  d[20095387] = "王五"   fmt.Println(d) }  

标签:map,string,int,fmt,Println,三种,go,make
来源: https://www.cnblogs.com/chenweihao/p/16503823.html