英文:
How can the make function take three parameters?
问题
函数make
的描述如下:
func make(Type, size IntegerType) Type
当我们在切片中使用make
时,有时会显示如下:
make([]int, 0, 10)
所以我的问题是:
make
函数如何接受三个参数?size IntegerType
不是可变参数。我感到困惑...
英文:
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...
答案1
得分: 13
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
可以有第三个参数:
切片:size指定长度。切片的容量等于其长度。可以提供第二个整数参数来指定不同的容量;它必须不小于长度,因此make([]int, 0, 10)分配了长度为0、容量为10的切片。
你还可以注意到make
也可以在没有整数参数的情况下使用:
映射:根据大小进行初始分配,但结果映射的长度为0。大小可以省略,在这种情况下,将分配一个较小的起始大小。
通道:通道的缓冲区根据指定的缓冲区容量进行初始化。如果为零,或者大小被省略,则通道是无缓冲的。
英文:
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.
答案2
得分: 7
make()
函数不是一个普通的函数,它是语言规范的一部分,是一个内置函数。在builtin
包中看到的builtin.make()
只是用于文档目的。那不是函数的实际签名。第三个可选参数是容量,只有在创建切片时才能提供。
在规范中有描述:创建切片、映射和通道:
make(T, n) 切片 类型为T,长度为n,容量为n的切片
make(T, n, m) 切片 类型为T,长度为n,容量为m的切片
并且在切片类型:中也提到:
> 使用内置函数make
可以创建给定元素类型T
的新初始化切片值,该函数接受切片类型和参数来指定长度和可选的容量。使用make
创建的切片总是分配一个新的隐藏数组,返回的切片值引用该数组。也就是说,执行以下代码:
>
> make([]T, length, capacity)
>
> 会产生与分配数组并对其进行切片相同的切片,因此这两个表达式是等价的:
>
> make([]int, 50, 100)
> new([100]int)[0:50]
英文:
The make()
function is not a regular function, is a builtin function being part of the language specification. What you see in the builtin
package (builtin.make()
) is only for documentation purposes. That is not the actual signature of the function. The 3rd optional parameter is the capacity, which may only be provided when you're creating a slice.
It's described in the spec: Making slices, maps and channels:
make(T, n) slice slice of type T with length n and capacity n
make(T, n, m) slice slice of type T with length n and capacity m
And also mentioned at Slice types:
> A new, initialized slice value for a given element type T
is made using the built-in function make
, which takes a slice type and parameters specifying the length and optionally the capacity. A slice created with make always allocates a new, hidden array to which the returned slice value refers. That is, executing
>
> make([]T, length, capacity)
>
> produces the same slice as allocating an array and slicing it, so these two expressions are equivalent:
>
> make([]int, 50, 100)
> new([100]int)[0:50]
答案3
得分: 1
函数make
是一个内置函数。该函数具有其他函数不具备的几个特点。其中一个特点是它可以接受可变数量的参数,正如你所指出的。另一个特点是它的第一个参数是一个类型。
函数定义func make(Type, size IntegerType) Type
仅用于文档目的,它并不是函数的实际定义。
英文:
The function make
is a builtin function. The function has several features not available to other functions. One is that it takes a variable number arguments as you noted. Another is that the first argument is a type.
The function definition func make(Type, size IntegerType) Type
is for documentation purposes only. It is not the actual definition of the function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论