Go語言中 new 和 make 是兩個內(nèi)置函數(shù),主要用來創(chuàng)建并分配類型的內(nèi)存蹂随。
在我們定義變量的時候十嘿,可能會覺得有點迷惑,不知道應(yīng)該使用哪個函數(shù)來聲明變量岳锁,其實他們的規(guī)則很簡單:
new 只分配內(nèi)存;
而 make 只能用于 slice绩衷、map 和 channel 的初始化;
下面我們就來具體介紹一下。
new
在Go語言中激率,new 函數(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ù)只接受一個參數(shù),這個參數(shù)是一個類型乒躺,并且返回一個指向該類型內(nèi)存地址的指針招盲。
同時 new 函數(shù)會把分配的內(nèi)存置為零,也就是類型的零值嘉冒。
【示例】使用 new 函數(shù)為變量分配內(nèi)存空間曹货。
var sum *int
sum = new(int) //分配空間
*sum = 98
fmt.Println(*sum)
當(dāng)然,new 函數(shù)不僅僅能夠為系統(tǒng)默認(rèn)的數(shù)據(jù)類型讳推,分配空間顶籽,自定義類型也可以使用 new 函數(shù)來分配空間,如下所示:
type Student struct {
name string
age int
}
var s *Student
s = new(Student) //分配空間
s.name ="dequan"
fmt.Println(s)
這里如果我們不使用 new 函數(shù)為自定義類型分配空間(將第 7 行注釋)银觅,就會報錯:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x80bd277]
goroutine 1 [running]:
這就是 new 函數(shù)礼饱,它返回的永遠(yuǎn)是類型的指針,指針指向分配類型的內(nèi)存地址设拟。
make
make 也是用于內(nèi)存分配的慨仿,但是和 new 不同久脯,它只用于 chan纳胧、map 以及 slice 的內(nèi)存創(chuàng)建,而且它返回的類型就是這三個類型本身帘撰,而不是他們的指針類型跑慕,因為這三種類型就是引用類型,所以就沒有必要返回他們的指針了摧找。
在Go語言中核行,make 函數(shù)的描述如下:
// The make built-in function allocates(分配) and initializes(初始化) an object of type
// slice, map, or chan (only). Like new(正如 new 關(guān)鍵字), the first argument is a type(第一個參數(shù)是一個類型), not a value (而不是一個值)。Unlike new(又不像 new 關(guān)鍵字), make's return type is the same as the type of its
// argument, not a pointer to it. (返回參數(shù)類型蹬耘,而不是該類型的指針)
// The specification of the result depends on the type:(返回的結(jié)果依賴于類型)
// Slice(切片): The size specifies the length(size 參數(shù)指定切片的長度). The capacity of the slice is equal to its length(切片的容量等于切片的長度). A second integer argument may be provided to specify a different capacity(可以使用第二個整型參數(shù)來指定切片的容量); 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
t:類型
size:可變參數(shù)芝雪,整型
-
切片:
make([]int, 0, 10)
- 一個size 參數(shù)時,則視為指定切片的長度和容量相等综苔,均為 size
- 兩個 size 參數(shù)時惩系,第一個為切片的長度位岔,第二個為切片的容量。
-
字典:一個空字典被分配足夠的空間去容納指定參數(shù)的元素堡牡。
- size參數(shù)可以省略,這種情況下分配一個小的空間給字典晤柄。
通道:分配通道緩沖區(qū)的大小為指定的大小擦剑,如果沒有指定,則通道沒有緩沖區(qū)芥颈。
通過上面的代碼可以看出 make 函數(shù)的 t 參數(shù)必須是 chan(通道)惠勒、map(字典)、slice(切片)中的一個爬坑,并且返回值也是類型本身捉撮。
注意:make 函數(shù)只用于 map,slice 和 channel妇垢,并且不返回指針巾遭。如果想要獲得一個顯式的指針,可以使用 new 函數(shù)進(jìn)行分配闯估,或者顯式地使用一個變量的地址灼舍。
Go語言中的 new 和 make 主要區(qū)別如下:
- make 只能用來分配及初始化類型為 slice、map涨薪、chan 的數(shù)據(jù)骑素。new 可以分配任意類型的數(shù)據(jù);
- new 分配返回的是指針刚夺,即類型
*Type
献丑。make 返回引用,即Type
侠姑; - new 分配的空間被清零创橄。make 分配空間后,會進(jìn)行初始化莽红。
實現(xiàn)原理
接下來我們將分別介紹一下 make 和 new 在初始化不同數(shù)據(jù)結(jié)構(gòu)時的具體過程妥畏,我們會從編譯期間和運行時兩個不同的階段理解這兩個關(guān)鍵字的原理。
make
我們已經(jīng)了解了 make 在創(chuàng)建 slice安吁、map 和 channel 的具體過程醉蚁,所以在這里我們也只是會簡單提及 make 相關(guān)的數(shù)據(jù)結(jié)構(gòu)初始化原理。
在編譯期的類型檢查階段鬼店,Go語言其實就將代表 make 關(guān)鍵字的 OMAKE 節(jié)點根據(jù)參數(shù)類型的不同轉(zhuǎn)換成了 OMAKESLICE网棍、OMAKEMAP 和 OMAKECHAN 三種不同類型的節(jié)點,這些節(jié)點最終也會調(diào)用不同的運行時函數(shù)來初始化數(shù)據(jù)結(jié)構(gòu)妇智。
new
內(nèi)置函數(shù) new 會在編譯期的 SSA 代碼生成階段經(jīng)過 callnew 函數(shù)的處理滥玷,如果請求創(chuàng)建的類型大小是 0捌锭,那么就會返回一個表示空指針的 zerobase 變量,在遇到其他情況時會將關(guān)鍵字轉(zhuǎn)換成 newobject:
func callnew(t *types.Type) *Node {
if t.NotInHeap() {
yyerror("%v is go:notinheap; heap allocation disallowed", t)
}
dowidth(t)
if t.Size() == 0 {
z := newname(Runtimepkg.Lookup("zerobase"))
z.SetClass(PEXTERN)
z.Type = t
return typecheck(nod(OADDR, z, nil), ctxExpr)
}
fn := syslook("newobject")
fn = substArgTypes(fn, t)
v := mkcall1(fn, types.NewPtr(t), nil, typename(t))
v.SetNonNil(true)
return v
}
需要提到的是罗捎,哪怕當(dāng)前變量是使用 var 進(jìn)行初始化观谦,在這一階段也可能會被轉(zhuǎn)換成 newobject 的函數(shù)調(diào)用并在堆上申請內(nèi)存:
func walkstmt(n *Node) *Node {
switch n.Op {
case ODCL:
v := n.Left
if v.Class() == PAUTOHEAP {
if prealloc[v] == nil {
prealloc[v] = callnew(v.Type)
}
nn := nod(OAS, v.Name.Param.Heapaddr, prealloc[v])
nn.SetColas(true)
nn = typecheck(nn, ctxStmt)
return walkstmt(nn)
}
case ONEW:
if n.Esc == EscNone {
r := temp(n.Type.Elem())
r = nod(OAS, r, nil)
r = typecheck(r, ctxStmt)
init.Append(r)
r = nod(OADDR, r.Left, nil)
r = typecheck(r, ctxExpr)
n = r
} else {
n = callnew(n.Type.Elem())
}
}
}
當(dāng)然這也不是絕對的,如果當(dāng)前聲明的變量或者參數(shù)不需要在當(dāng)前作用域外生存桨菜,那么其實就不會被初始化在堆上豁状,而是會初始化在當(dāng)前函數(shù)的棧中并隨著函數(shù)調(diào)用的結(jié)束而被銷毀。
newobject 函數(shù)的工作就是獲取傳入類型的大小并調(diào)用 mallocgc 在堆上申請一片大小合適的內(nèi)存空間并返回指向這片內(nèi)存空間的指針:
func newobject(typ *_type) unsafe.Pointer {
return mallocgc(typ.size, typ, true)
}
總結(jié)
最后倒得,簡單總結(jié)一下Go語言中 make 和 new 關(guān)鍵字的實現(xiàn)原理泻红,make 關(guān)鍵字的主要作用是創(chuàng)建 slice、map 和 Channel 等內(nèi)置的數(shù)據(jù)結(jié)構(gòu)霞掺,而 new 的主要作用是為類型申請一片內(nèi)存空間谊路,并返回指向這片內(nèi)存的指針。