ITPub博客

首页 > IT职业 > IT生活 > Go中map的创建和初始化

Go中map的创建和初始化

IT生活 作者:ghost_hand 时间:2021-09-09 10:17:05 0 删除 编辑


// code_014_map_usage project main.go

package main

import (

    "fmt"

)

func main() {

   

    //1、创建

    var g1 map[int]string //默认值为nil

    g2 := map[int]string{}

    g3 := make(map[int]string)

    g4 := make(map[int]string, 10)

    fmt.Println(g1, g2, g3, g4, len(g4))

    //2、初始化

    var m1 map[int]string = map[int]string{1: "ck_god", 2: "god_girl"}

    m2 := map[int]string{1: "ck_god", 2: "god_girl"}

    fmt.Println(m1, m2)

}

结果如下:

//map[] map[] map[] map[] 0

//map[1:ck_god 2:god_girl] map[1:ck_god 2:god_girl]

©著作权归作者所有:来自51CTO博客作者ck_god的原创作品,如需转载,请注明出处,否则将追究法律责任


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1059/viewspace-2819545/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    81
  • 访问量
    444