1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| PS F:\Hugos\go_docs> go doc builtin.make
package builtin // import "builtin"
func make(t Type, size ...IntegerType) Type
The make built-in function allocates and initializes an object of type
slice, map, or chan (only). Like new, the first argument is a type,
not a value. Unlike new, make's return type is the same as the type of its
argument, not a pointer to it. The specification of the result depends on
the type:
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. For example, make([]int, 0, 10) allocates an underlying array
of size 10 and returns a slice of length 0 and capacity 10 that is
backed by this underlying array.
Map: An empty map is allocated with enough space to hold the
specified number of elements. 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.
make 内置函数分配并初始化一个切片、映射或通道对象。与 new 类似,第一个参数是类型而不是值。与 new 不同的是,make 的返回类型与其参数类型相同,而不是该类型的指针。结果的规格取决于类型:
切片:size 指定长度。切片的容量等于其长度。可以提供第二个整数参数来指定不同的容量;它必须不小于长度。例如,make([]int, 0, 10) 分配一个大小为 10 的底层数组,并返回一个长度为 0、容量为 10 的切片,该切片由这个底层数组支持。
map:分配一个足以容纳指定数量元素的空映射。可以省略 size 参数,此情况下会分配一个小的初始大小。
通道:通道的缓冲区容量以 size 指定。如果为零或省略 size,通道为无缓冲通道。
|