在golang中,make和new都是分配記憶體的,但是它們之間還是有些區別的,只有理解了它們之間的不同,才能在合適的場合使用,
簡單來說,new只是分配記憶體,不初始化記憶體; 而make即分配又初始化記憶體,所謂的初始化就是給型別賦初值,比如字符為空,整型為0, 邏輯值為false等,
new
先看下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
可以看出,它的引數是一個型別,回傳值為指向該型別記憶體地址的指標,同時會把分配的記憶體置為零,也就是型別的零值, 即字符為空,整型為0, 邏輯值為false
看幾個new的示例
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) //結構
fmt.Println(a, " ", *a)
fmt.Println(s, " ",*s)
fmt.Println(b, " ",*b)
fmt.Println(i, " ",*i)
fmt.Println(ps, " ", *ps)
輸出結果如下
&[0 0] [0 0]
0xc00000e1e0
0xc00001a07a false
0xc00001a090 0
&{ 0} { 0}
上面示例是基本的型別,再看下slice, map,chan這些用new咋操作
map操作
var mp *map[string]string
mp = new(map[string]string)
//*mp = make(map[string]string) //這行注掉會panic "panic: assignment to entry in nil map""
(*mp)["name"] = "lc"
fmt.Println((*mp)["name"])
slice操作
var ms *[]string
ms = new([]string)
//*ms = make([]string,5) //這行注掉會pance "panic: runtime error: index out of range"
(*ms)[0] = "lc"
fmt.Println((*ms)[0])
上面可以看出,silce、map、channel等型別屬于參考型別,參考型別初始化為nil,nil是不能直接賦值的,也不能用new分配記憶體,還需要使用make來分配,
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.
func make(t Type, size ...IntegerType) Type
可以看出,它回傳的就是型別本身,而不是指標型別,因為make只能給slice,map,channel等初始化記憶體,它們回傳的就是參考型別,那么就沒必要回傳指標了
看下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)
小結
make 僅用來分配及初始化型別為 slice、map、chan 的資料,new 可分配任意型別的資料.
new 分配回傳的是指標,即型別 *Type,make 回傳參考,即 Type.
new 分配的空間被清零, make 分配空間后,會進行初始化.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/55027.html
標籤:Go
上一篇:goweb- 對請求的處理
