原文鏈接: Go 語言 new 和 make 關(guān)鍵字的區(qū)別
本篇文章來介紹一道非常常見的面試題他挎,到底有多常見呢捡需?可能很多面試的開場(chǎng)白就是由此開始的办桨。那就是 new 和 make 這兩個(gè)內(nèi)置函數(shù)的區(qū)別呢撞。
其實(shí)這個(gè)問題本身并不復(fù)雜汰蓉,簡(jiǎn)單來說就是绷蹲,new 只分配內(nèi)存祝钢,而 make 只能用于 slice若厚、map 和 chan 的初始化拦英,下面我們就來詳細(xì)介紹一下。
new
new 是一個(gè)內(nèi)置函數(shù)测秸,它會(huì)分配一段內(nèi)存疤估,并返回指向該內(nèi)存的指針。
其函數(shù)簽名如下:
源碼
// 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
從上面的代碼可以看出乞封,new 函數(shù)只接受一個(gè)參數(shù)做裙,這個(gè)參數(shù)是一個(gè)類型,并且返回一個(gè)指向該類型內(nèi)存地址的指針肃晚。
同時(shí) new 函數(shù)會(huì)把分配的內(nèi)存置為零锚贱,也就是類型的零值。
使用
使用 new 函數(shù)為變量分配內(nèi)存空間:
p1 := new(int)
fmt.Printf("p1 --> %#v \n ", p1) //(*int)(0xc42000e250)
fmt.Printf("p1 point to --> %#v \n ", *p1) //0
var p2 *int
i := 0
p2 = &i
fmt.Printf("p2 --> %#v \n ", p2) //(*int)(0xc42000e278)
fmt.Printf("p2 point to --> %#v \n ", *p2) //0
上面的代碼是等價(jià)的关串,new(int)
將分配的空間初始化為 int 的零值拧廊,也就是 0监徘,并返回 int 的指針,這和直接聲明指針并初始化的效果是相同的吧碾。
當(dāng)然凰盔,new 函數(shù)不僅能夠?yàn)橄到y(tǒng)默認(rèn)的數(shù)據(jù)類型分配空間,自定義類型也可以使用 new 函數(shù)來分配空間倦春,如下所示:
type Student struct {
name string
age int
}
var s *Student
s = new(Student) //分配空間
s.name = "zhangsan"
fmt.Println(s)
這就是 new 函數(shù)户敬,它返回的永遠(yuǎn)是類型的指針,指針指向分配類型的內(nèi)存地址睁本。需要注意的是尿庐,new 函數(shù)只會(huì)分配內(nèi)存空間,但并不會(huì)初始化該內(nèi)存空間呢堰。
make
make 也是用于內(nèi)存分配的抄瑟,但是和 new 不同,它只用于 slice枉疼、map 和 chan 的內(nèi)存創(chuàng)建皮假,而且它返回的類型就是這三個(gè)類型本身,而不是他們的指針類型骂维。因?yàn)檫@三種類型本身就是引用類型惹资,所以就沒有必要返回他們的指針了。
其函數(shù)簽名如下:
源碼
// 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, so make([]int, 0, 10) allocates a slice of length 0 and
// capacity 10.
// 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
通過上面的代碼可以看出 make 函數(shù)的 t
參數(shù)必須是 slice席舍、map 和 chan 中的一個(gè)布轿,并且返回值也是類型本身哮笆。
使用
下面用 slice 來舉一個(gè)例子:
var s1 []int
if s1 == nil {
fmt.Printf("s1 is nil --> %#v \n ", s1) // []int(nil)
}
s2 := make([]int, 3)
if s2 == nil {
fmt.Printf("s2 is nil --> %#v \n ", s2)
} else {
fmt.Printf("s2 is not nill --> %#v \n ", s2)// []int{0, 0, 0}
}
slice 的零值是 nil
来颤,但使用 make 初始化之后,slice 內(nèi)容被類型 int 的零值填充稠肘,如:[]int{0, 0, 0}
福铅。
map 和 chan 也是類似的,就不多說了项阴。
總結(jié)
通過以上分析滑黔,總結(jié)一下 new 和 make 主要區(qū)別如下:
- make 只能用來分配及初始化類型為 slice、map 和 chan 的數(shù)據(jù)环揽。new 可以分配任意類型的數(shù)據(jù)略荡;
- new 分配返回的是指針,即類型
*Type
歉胶。make 返回類型本身汛兜,即Type
; - new 分配的空間被清零通今。make 分配空間后粥谬,會(huì)進(jìn)行初始化肛根;
以上就是本文的全部?jī)?nèi)容,如果覺得還不錯(cuò)的話歡迎點(diǎn)贊漏策,轉(zhuǎn)發(fā)和關(guān)注派哲,感謝支持。
參考文章:
- https://go.dev/doc/effective_go#allocation_new
- http://c.biancheng.net/view/5722.html
- https://sanyuesha.com/2017/07/26/go-make-and-new/
推薦閱讀: