百度手机网站优化指南,网站群的建设目标,vs2010做网站,程序员学历要求高吗1. 先看一个例子
package mainimport fmtfunc main() {var a *int*a 10fmt.Println(*a)
}运行结果是啥呢#xff1f;
问#xff1a;为什么会报这个panic呢#xff1f; 答#xff1a;因为如果是一个引用类型#xff0c;我们不仅要声明它#xff0c;还要为…
1. 先看一个例子
package mainimport fmtfunc main() {var a *int*a 10fmt.Println(*a)
}运行结果是啥呢
问为什么会报这个panic呢 答因为如果是一个引用类型我们不仅要声明它还要为它分配内存空间否则我们赋值的 10 就无处安放。值类型的声明不需要我们分配内存空间因为已经默认给我们分配好啦。
问那么如何分配内存呢 答在使用a这个引用类型的变量之前给它分配内存内存即可。
package mainimport fmtfunc main() {var a *inta new(int)*a 10fmt.Println(*a)
}问听说对变量分配内存有new和make函数两种我应该在对各种变量初始化和分配内存时用哪个呢 答在golang中的源码中已经做出了解释可以接着看后面的内容。
2. 源码中的描述
make函数用来对slice、map、和channel类型的初始化和分配内存。和new函数一样第一个参数是一个类型而不是值和new函数不一样的是make函数的返回类型和传入参数的类型一致而new函数的返回类型是传入类型的指针。特定的结果依赖于传入的类型
slicesize指定切片的长度容量在不指定的情况下和长度相等。如果额外指定容量则容量可能和长度不相等。比如make([]int,0,10)分配了一个大小为10的底层数组并返回一个长度为0、容量为10的切片该切片由这个底层数组支持。map一个空的map被分配了足够的空间以容纳指定数量的元素。如果省略了大小则会分配一个较小的起始大小。通道的缓冲区使用指定的缓冲区容量进行初始化。如果容量为0或者未指定大小则通道是无缓冲的。
new函数用于分配内存。其第一个参数是一个类型而不是一个值返回的值是指向新分配的内存的指针该内存的内容是该类型的零值。
// 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, makes 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 channels 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// 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 3. 两者的异同
相同点
new 和 make 都是用于内存的分配。new 用于给类型分配内存空间并且变量的值为零。
不同点
make 只用于 chanmapslice 的初始化。make 返回类型本身new 返回指向类型的指针。