golang 函数传多个参数,golang如何使函数可以接受三个参数?

The function make is describe like this:

func make(Type, size IntegerType) Type

When we use make for slicing sometimes it shows like:

make([]int, 0, 10)

So my question is:

How can the make function take three parameters? The size IntegerType is not Vararg. I'm confused...

解决方案

The make function is one of a bunch of built-in functions that are allowed to do things that you cannot achieve (at least not cleanly and easily) in your Go code.

It has a number of overloaded forms for creating maps, channels and slices (see

https://golang.org/ref/spec#Making_slices_maps_and_channels) :

Your confusion probably stems from https://golang.org/pkg/builtin/#make which shows make as having the signature func make(Type, size IntegerType) Type.

If you look closer in that section, you would also see an indication that make can have a third argument:

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, 0, 10) allocates a slice of length 0 and capacity 10.

You can also notice that make can also be used without its integer argument:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值