Golang面試make和new的用法
在golang中,make和new都分配內(nèi)存,但是它們之間仍然存在一些差異。只有了解它們之間的差異,才能在適當(dāng)?shù)膱?chǎng)合使用它們。
簡(jiǎn)而言之,new只是分配內(nèi)存,而不初始化內(nèi)存;make分配并初始化內(nèi)存。所謂的初始化就是給一個(gè)類型賦一個(gè)初始值,例如,字符為空,整數(shù)為0,邏輯值為false。
從Golang的官方文檔的builtin(內(nèi)置的軟件包)中可以找到,make和new的用法。

官方文檔的內(nèi)置的軟件包
new的使用介紹:
我們先來(lái)看看new的定義
- // 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.
- //內(nèi)建函數(shù)new分配內(nèi)存。其第一個(gè)實(shí)參為類型,而非值。其返回值為指向該類型的新分配的零值的指針。
- func new(Type) *Type
可以看出,它的參數(shù)是一個(gè)類型,返回值是指向該類型的內(nèi)存地址的指針,并且分配的內(nèi)存將被設(shè)置為零,即該類型的零值,即字符為空,整數(shù)為0,邏輯值為false
看一些例子
- type P struct {
- Name string
- Age int
- } var a *[2]int
- var s *string
- var b *bool
- var i *int
- var ps *P
- a = new([2]int)
- s = new(string)
- b = new(bool)
- i = new(int)
- ps = new(P) //structure
- fmt.Println(a, " ", *a)
- fmt.Println(s, " ", *s)
- fmt.Println(b, " ", *b)
- fmt.Println(i, " ", *i)
- fmt.Println(ps, " ", *ps)
輸出如下:
- &[0 0] [0 0]
- 0xc0000821e0
- 0xc0000a409a false
- 0xc0000a40b0 0
- &{ 0} { 0}
上面基礎(chǔ)類型,我們看一下slice, map and channel類型是如何操作的:
- //map 操作
- var mp *map[string]string
- mp = new(map[string]string)
- //注釋掉下面的行,new map 返回為nil,直接使用會(huì)panic
- //*mp = make(map[string]string) // if this line is omitted, it will pan "Pan: assignment to entry in nil map"“
- (*mp)["name"] = "lc"
- fmt.Println((*mp)["name"])
- // slice 操作 var ms *[]string
- ms = new([]string)
- // 注釋掉下面的行訪問(wèn)的時(shí)候會(huì)下標(biāo)超出范圍 //*ms = make([]string,5) // if this line is deleted, it will "panic: runtime error: index out of range"
- (*ms)[0] = "lc"
- fmt.Println((*ms)[0])
從上面可以看出,silce,map,channel和其他類型是引用類型。當(dāng)引用類型初始化為nil時(shí),不能直接分配nil,也不能使用new來(lái)分配內(nèi)存,還需要使用make來(lái)進(jìn)行分配。
make的使用介紹:
我們看一下make的定義
- / /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.
- //切片:size指定了其長(zhǎng)度。該切片的容量等于其長(zhǎng)度。切片支持第二個(gè)整數(shù)實(shí)參可用來(lái)指定不同的容量; 它必須不小于其長(zhǎng)度,因此 make([]int, 0, 10) 會(huì)分配一個(gè)長(zhǎng)度為0,容量為10的切片。
- //映射:初始分配的創(chuàng)建取決于size,但產(chǎn)生的映射長(zhǎng)度為0。size可以省略,這種情況下就會(huì)分配一個(gè)小的起始大小。
- //通道:通道的緩存根據(jù)指定的緩存容量初始化。若 size為零或被省略,該信道即為無(wú)緩存的。
- func make(t Type, size ...IntegerType) Type
可以看出,它返回的是類型本身而不是指針類型,因?yàn)閙ake只能為slice,map,channel等初始化內(nèi)存,并且它們返回引用類型,因此不必返回指針
讓我們看一些make的例子:
- mm :=make(map[string]string)
- mm["name"] = "lc"
- fmt.Println(mm["name"])
- mss :=make([]int,2)
- mss[0] = 100
- fmt.Println(mss[0])
- ch :=make(chan int,1)
- ch <-100
- fmt.Println(<-ch)
總結(jié):
make僅用于分配和初始化slice,map和chan類型的數(shù)據(jù)。new可以分配任何類型的數(shù)據(jù)。new分配返回一個(gè)指針,即Type * Type。make返回一個(gè)引用,該引用為Type由make分配的空間之后,清除并初始化由new分配的空間。