接口声明的区别 [英] Interface declaration difference

查看:140
本文介绍了接口声明的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个初学者gopher,我很努力去理解之间的区别:

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

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

我试图找到一个解释,但找不到任何东西。

解决方案

两者是相同的,只是当使用<$ c

例如,你可以(可选地)指定你想要初始化的地图的大小。


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



另外,当你像这样初始化地图文字时 -

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

然后在那里。在这种情况下,您已将其初始化为 nil 地图。如果您希望创建一个您希望能够在将来添加事物的地图(即 append ),则应该使用第一个方法或将其初始化为

  var m 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.

解决方案

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{}

这篇关于接口声明的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆