new 和 make 是兩個內置函式,主要用來創建并分配型別的記憶體,在我們定義變數的時候,可能會覺得有點迷惑,不知道應該使用哪個函式來宣告變數,其實他們的規則很簡單,
new 只分配記憶體,
make 只能用于 slice、map 和 channel 的初始化,
下面我們就來具體介紹一下:
new
在Go語言中,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.func new(Type) *Type從上面的代碼可以看出,new 函式只接受一個引數,這個引數是一個型別,并且回傳一個指向該型別記憶體地址的指標,同時 new 函式會把分配的記憶體置為零,也就是型別的零值,
【示例】使用 new 函式為變數分配記憶體空間,
var sum *intsum = new(int) //分配空間*sum = 98fmt.Println(*sum)當然,new 函式不僅僅能夠為系統默認的資料型別,分配空間,自定義型別也可以使用 new 函式來分配空間,如下所示:
type Student struct {name stringage int}var s *Students = new(Student) //分配空間s.name ="dequan"fmt.Println(s)這里如果我們不使用 new 函式為自定義型別分配空間(將第 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 函式,它回傳的永遠是型別的指標,指標指向分配型別的記憶體地址,
make
make 也是用于記憶體分配的,但是和 new 不同,它只用于 chan、map 以及 slice 的記憶體創建,而且它回傳的型別就是這三個型別本身,而不是他們的指標型別,因為這三種型別就是參考型別,所以就沒有必要回傳他們的指標了,在Go語言中,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, 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 函式的 t 引數必須是 chan(通道)、map(字典)、slice(切片)中的一個,并且回傳值也是型別本身,
注意:make 函式只用于 map,slice 和 channel,并且不回傳指標,如果想要獲得一個顯式的指標,可以使用 new 函式進行分配,或者顯式地使用一個變數的地址,
Go語言中的 new 和 make 主要區別如下:- make 只能用來分配及初始化型別為 slice、map、chan 的資料,new 可以分配任意型別的資料;
- new 分配回傳的是指標,即型別 *Type,make 回傳參考,即 Type;
- new 分配的空間被清零,make 分配空間后,會進行初始化;
總結
最后,簡單總結一下Go語言中 make 和 new 關鍵字的實作原理,make 關鍵字的主要作用是創建 slice、map 和 Channel 等內置的資料結構,而 new 的主要作用是為型別申請一片記憶體空間,并回傳指向這片記憶體的指標,轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/56902.html
標籤:Go
上一篇:Go語言nil:空值/零值
