企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
36. #### Go中new和make的区别在Go中,的值类型和引用类型: ~~~gfm 值类型:int,float,bool,string,struct和array. 变量直接存储值,分配栈区的内存空间,这些变量所占据的空间在函数被调用完后会自动释放。 引用类型:slice,map,chan和值类型对应的指针. 变量存储的是一个地址(或者理解为指针),指针指向内存中真正存储数据的首地址。内存通常在堆上分配,通过GC回收。 ~~~ 这里需要注意的是: 对于引用类型的变量,我们不仅要声明变量,更重要的是,我们得手动为它分配空间. 因此new该方法的参数要求传入一个类型,而不是一个值,它会申请一个该类型大小的内存空间,并会初始化为对应的零值,返回指向该内存空间的一个指针。 ~~~go // The new built-in function allocates memory. The first argument is a type, // not a value, and the value returned is a pointer to a newly // allocated zero value of that type. func new(Type) *Type ~~~ 而make也是用于内存分配,但是和new不同,只用来引用对象slice、map和channel的内存创建,它返回的类型就是类型本身,而不是它们的指针类型。 make(\[\]int, 2, 4) 第三种用法,第二参数指定的是切片的长度,第三个参数是用来指定预留的空间长度,例如a := make(\[\]int, 2, 4), 这里值得注意的是返回的切片a的总长度是4,预留的意思并不是另外多出来4的长度,其实是包含了前面2个已经切片的个数的。所以举个例子当你这样用的时候 a := make(\[\]int, 4, 2),就会报语法错误。 ~~~ // 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. func make(t Type, size ...IntegerType) Type ~~~