英文:
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{}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论