Golang new和make的区别(十三)
Go提供了两种分配原语,即new和make。它们所做的事情是不一样的,所应用的类型也不同。
new函数
new是内置函数,函数的原型为:
func new(Type) *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.
new用来分配内存,但与其他语言中的同名函数不同,它不会初始化内存,只会将内存置零;也就是说,new(T)会为类型为T的新项分配已置零的内存空间,并返回他的地址,也就是一个类型为*T的值。用Go的术语来说,它返回一个指针,改指针指向新分配的,类型为T的零值;
make函数
make也是内置函数,原型为:
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:
make的目的不同于new,它只用于内建类型(slice、map和channel)的创建,并返回类型为T(非指针)的已初始化(非零值)的值;出现这种差异的原因在于,这三种类型本质上为引用类型,它们在使用前必须初始化;
new和make都在堆上分配内存,但是它们的行为不同,适用于不同的类型。
new(T)为每个新的类型T分配一片内存,初始化为0并且返回类型为*T的内存地址:这种方法返回一个指向类型为T,值为0的地址的指针,它适用于值类型如数组和结构体;它相当于&T{}。
make(T)返回一个类型为T的初始值,它只适用于3种内建的引用类型:slice、map和channel。
换言之,new函数分配内存,make函数初始化:
package main
import "fmt"
func main() {
p := new([]int) //p == nil; with len and cap 0
fmt.Println(p)
v := make([]int, 10, 50) // v is initialed with len 10, cap 50
fmt.Println(v)
// (*p)[0] = 18 // panic: runtime error: index out of range,because p is a nil pointer, with len and cap 0
// v[1] = 18 // ok
}
打印出的结果:
&[]
[0 0 0 0 0 0 0 0 0 0]
- 具体有关内置函数的介绍可以参考go官方文档
- 邮箱 :charon.chui@gmail.com
- Good Luck!