|
| 1 | +**原文链接:** [Go 语言 new 和 make 关键字的区别](https://mp.weixin.qq.com/s/NBDkI3roHgNgW1iW4e_6cA) |
| 2 | + |
| 3 | +本篇文章来介绍一道非常常见的面试题,到底有多常见呢?可能很多面试的开场白就是由此开始的。那就是 new 和 make 这两个内置函数的区别。 |
| 4 | + |
| 5 | +其实这个问题本身并不复杂,简单来说就是,new 只分配内存,而 make 只能用于 slice、map 和 chan 的初始化,下面我们就来详细介绍一下。 |
| 6 | + |
| 7 | +## new |
| 8 | + |
| 9 | +new 是一个内置函数,它会分配一段内存,并返回指向该内存的指针。 |
| 10 | + |
| 11 | +其函数签名如下: |
| 12 | + |
| 13 | +### 源码 |
| 14 | + |
| 15 | +```go |
| 16 | +// The new built-in function allocates memory. The first argument is a type, |
| 17 | +// not a value, and the value returned is a pointer to a newly |
| 18 | +// allocated zero value of that type. |
| 19 | +func new(Type) *Type |
| 20 | +``` |
| 21 | + |
| 22 | +从上面的代码可以看出,new 函数只接受一个参数,这个参数是一个类型,并且返回一个指向该类型内存地址的指针。 |
| 23 | + |
| 24 | +同时 new 函数会把分配的内存置为零,也就是类型的零值。 |
| 25 | + |
| 26 | +### 使用 |
| 27 | + |
| 28 | +使用 new 函数为变量分配内存空间: |
| 29 | + |
| 30 | +```go |
| 31 | +p1 := new(int) |
| 32 | +fmt.Printf("p1 --> %#v \n ", p1) //(*int)(0xc42000e250) |
| 33 | +fmt.Printf("p1 point to --> %#v \n ", *p1) //0 |
| 34 | + |
| 35 | +var p2 *int |
| 36 | +i := 0 |
| 37 | +p2 = &i |
| 38 | +fmt.Printf("p2 --> %#v \n ", p2) //(*int)(0xc42000e278) |
| 39 | +fmt.Printf("p2 point to --> %#v \n ", *p2) //0 |
| 40 | +``` |
| 41 | + |
| 42 | +上面的代码是等价的,`new(int)` 将分配的空间初始化为 int 的零值,也就是 0,并返回 int 的指针,这和直接声明指针并初始化的效果是相同的。 |
| 43 | + |
| 44 | +当然,new 函数不仅能够为系统默认的数据类型分配空间,自定义类型也可以使用 new 函数来分配空间,如下所示: |
| 45 | + |
| 46 | +```go |
| 47 | +type Student struct { |
| 48 | + name string |
| 49 | + age int |
| 50 | +} |
| 51 | +var s *Student |
| 52 | +s = new(Student) //分配空间 |
| 53 | +s.name = "zhangsan" |
| 54 | +fmt.Println(s) |
| 55 | +``` |
| 56 | + |
| 57 | +这就是 new 函数,它返回的永远是类型的指针,指针指向分配类型的内存地址。需要注意的是,new 函数只会分配内存空间,但并不会初始化该内存空间。 |
| 58 | + |
| 59 | +## make |
| 60 | + |
| 61 | +make 也是用于内存分配的,但是和 new 不同,它只用于 slice、map 和 chan 的内存创建,而且它返回的类型就是这三个类型本身,而不是他们的指针类型。因为这三种类型本身就是引用类型,所以就没有必要返回他们的指针了。 |
| 62 | + |
| 63 | +其函数签名如下: |
| 64 | + |
| 65 | +### 源码 |
| 66 | + |
| 67 | +```go |
| 68 | +// The make built-in function allocates and initializes an object of type |
| 69 | +// slice, map, or chan (only). Like new, the first argument is a type, not a |
| 70 | +// value. Unlike new, make's return type is the same as the type of its |
| 71 | +// argument, not a pointer to it. The specification of the result depends on |
| 72 | +// the type: |
| 73 | +// Slice: The size specifies the length. The capacity of the slice is |
| 74 | +// equal to its length. A second integer argument may be provided to |
| 75 | +// specify a different capacity; it must be no smaller than the |
| 76 | +// length, so make([]int, 0, 10) allocates a slice of length 0 and |
| 77 | +// capacity 10. |
| 78 | +// Map: An empty map is allocated with enough space to hold the |
| 79 | +// specified number of elements. The size may be omitted, in which case |
| 80 | +// a small starting size is allocated. |
| 81 | +// Channel: The channel's buffer is initialized with the specified |
| 82 | +// buffer capacity. If zero, or the size is omitted, the channel is |
| 83 | +// unbuffered. |
| 84 | +func make(t Type, size ...IntegerType) Type |
| 85 | +``` |
| 86 | + |
| 87 | +通过上面的代码可以看出 make 函数的 `t` 参数必须是 slice、map 和 chan 中的一个,并且返回值也是类型本身。 |
| 88 | + |
| 89 | +### 使用 |
| 90 | + |
| 91 | +下面用 slice 来举一个例子: |
| 92 | + |
| 93 | +```go |
| 94 | +var s1 []int |
| 95 | +if s1 == nil { |
| 96 | + fmt.Printf("s1 is nil --> %#v \n ", s1) // []int(nil) |
| 97 | +} |
| 98 | + |
| 99 | +s2 := make([]int, 3) |
| 100 | +if s2 == nil { |
| 101 | + fmt.Printf("s2 is nil --> %#v \n ", s2) |
| 102 | +} else { |
| 103 | + fmt.Printf("s2 is not nill --> %#v \n ", s2)// []int{0, 0, 0} |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +slice 的零值是 `nil`,但使用 make 初始化之后,slice 内容被类型 int 的零值填充,如:`[]int{0, 0, 0}`。 |
| 108 | + |
| 109 | +map 和 chan 也是类似的,就不多说了。 |
| 110 | + |
| 111 | +## 总结 |
| 112 | + |
| 113 | +通过以上分析,总结一下 new 和 make 主要区别如下: |
| 114 | + |
| 115 | +1. make 只能用来分配及初始化类型为 slice、map 和 chan 的数据。new 可以分配任意类型的数据; |
| 116 | +2. new 分配返回的是指针,即类型 `*Type`。make 返回类型本身,即 `Type`; |
| 117 | +3. new 分配的空间被清零。make 分配空间后,会进行初始化; |
| 118 | + |
| 119 | + |
| 120 | +以上就是本文的全部内容,如果觉得还不错的话欢迎**点赞**,**转发**和**关注**,感谢支持。 |
| 121 | + |
| 122 | +*** |
| 123 | + |
| 124 | +**参考文章:** |
| 125 | + |
| 126 | +- https://go.dev/doc/effective_go#allocation_new |
| 127 | +- http://c.biancheng.net/view/5722.html |
| 128 | +- https://sanyuesha.com/2017/07/26/go-make-and-new/ |
| 129 | + |
| 130 | +**推荐阅读:** |
| 131 | + |
| 132 | +- [为什么 Go 不支持 []T 转换为 []interface](https://mp.weixin.qq.com/s/cwDEgnicK4jkuNpzulU2bw) |
| 133 | +- [为什么 Go 语言 struct 要使用 tags](https://mp.weixin.qq.com/s/L7-TJ-CzYfuVrIBWP7Ebaw) |
0 commit comments