加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang make函数如何才能获得三个参数?

发布时间:2020-12-16 09:22:15 所属栏目:大数据 来源:网络整理
导读:函数make是这样描述的: func make(Type,size IntegerType) Type 当我们使用make进行切片时,它有时显示如下: make([]int,10) 所以我的问题是: make函数如何获取三个参数? IntegerType的大小不是Vararg.我糊涂了… 解决方法 make函数是一组内置函数之一,允
函数make是这样描述的:

func make(Type,size IntegerType) Type

当我们使用make进行切片时,它有时显示如下:

make([]int,10)

所以我的问题是:

make函数如何获取三个参数? IntegerType的大小不是Vararg.我糊涂了…

解决方法

make函数是一组内置函数之一,允许你在Go代码中做一些你无法实现的事情(至少不是干净利落和轻松).
它有许多重载形式,用于创建地图,通道和切片(请参阅
https://golang.org/ref/spec#Making_slices_maps_and_channels):

你的困惑可能源于https://golang.org/pkg/builtin/#make,它显示make具有签名func make(Type,size IntegerType)Type.
如果您仔细查看该部分,您还会看到make可以有第三个参数的指示:

Slice: The size specifies the length. The capacity of the slice is
equal to its length. A second integer argument may be provided to
specify a different capacity
; it must be no smaller than the length,
so make([]int,10) allocates a slice of length 0 and capacity 10.

您还可以注意到make也可以在没有整数参数的情况下使用:

Map: An initial allocation is made according to the size but the
resulting map has length 0. The size may be omitted,in which case a
small starting size is allocated.

Channel: The channel’s buffer is initialized with the specified buffer capacity. If zero,or the size is omitted,the channel is unbuffered.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读