使用/不使用创建地图 [英] Creating map with/without make
本文介绍了使用/不使用创建地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var m = map [string] int {}
和
var m = make(map [string] int)
第一个只是快速字段初始化的快捷方式吗?是否有性能方面的考虑?
解决方案
第二种形式总是创建一个空映射。 b
第一种形式是地图文字的一种特殊情况。地图文字允许创建非空地图:
m:= map [bool] string {false :FALSE,true:TRUE}
现在你的(泛化的)例子:
m:= map [T] U {}
是没有初始值(键/值对)的映射文字。它完全等同于:
m:= make(map [T] U)
What exactly is the difference between
var m = map[string]int{}
and
var m = make(map[string]int)
Is the first just a shortcut for faster field initialization? Are there performance considerations?
解决方案
The second form always creates an empty map.
The first form is a special case of a map literal. Map literals allow to create non empty maps:
m := map[bool]string{false: "FALSE", true: "TRUE"}
Now your (generalized) example:
m := map[T]U{}
is a map literal with no initial values (key/value pairs). It's completely equivalent to:
m := make(map[T]U)
这篇关于使用/不使用创建地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文