doumanshan6314 2018-12-01 22:43
浏览 689
已采纳

make(map)和map {}之间的区别

Just wondering what the difference is between:

z := make(map[*test] string)

and

z := map[*test] string{}

am I imagining things or are they both not valid?

  • 写回答

2条回答 默认 最新

  • doukuangxun5382 2018-12-01 23:24
    关注

    The Go Programming Language Specification

    Making slices, maps and channels

    The built-in function make takes a type T, which must be a slice, map or channel type, optionally followed by a type-specific list of expressions. It returns a value of type T (not *T). The memory is initialized as described in the section on initial values.

    Call         Type T  Result
    make(T)      map     map of type T
    make(T, n)   map     map of type T with initial space for approximately n elements
    

    Composite literals

    Composite literals construct values for structs, arrays, slices, and maps and create a new value each time they are evaluated. They consist of the type of the literal followed by a brace-bound list of elements. Each element may optionally be preceded by a corresponding key.

    map[string]int{}
    map[string]int{"one": 1}
    

    make is the canonical form. Composite literals are a convenient, alternate form.

    z := make(map[int]string)
    

    and

    z := map[int]string{}
    

    are equivalent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?