接口声明差异

huangapple go评论76阅读模式
英文:

Interface declaration difference

问题

作为一个初学者的gopher,我很难理解以下两者之间的区别:

m := map[string]interface{}{}

m := make(map[string]interface{})

我尝试找到解释,但没有找到任何相关的内容。

英文:

As a beginner gopher, i'm struggling to understand the difference between:

m := map[string]interface{}{}

and

m := make(map[string]interface{})

I tried to find an explanation, but couldn't find anything.

答案1

得分: 0

两者基本相同,只是在使用make时,你可以(当然是可选的)指定要初始化的地图的大小。

例如,

m := make(map[string]interface{}, 10)

此外,当你像这样初始化一个地图字面量时 -

m := map[string]interface{}{}

你可以立即在其中添加值。在这种情况下,你将其初始化为一个nil地图。如果你希望创建一个可以在将来添加东西的地图(即append),你应该使用第一种方法或将其初始化为

var m map[string]interface{}
英文:

Both are the same except that when using make, you can (of course optionally) specify the size of the map you're trying to initialize.

For e.g.,

m := make(map[string]interface{}, 10)

Also, when you initialize a map literal like this -

m := map[string]interface{}{}

you can add values to it then and there. In this case you have initialized it to a nil map. If you'd rather create a map in which you want to be able to add things in future (i.e. append), you should use either the first method or initialize it as

var m map[string]interface{}

huangapple
  • 本文由 发表于 2017年3月20日 01:26:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/42889715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定