自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go 語言 New 和 Make 關鍵字的區(qū)別

開發(fā) 前端
其實這個問題本身并不復雜,簡單來說就是,new 只分配內存,而 make 只能用于 slice、map 和 chan 的初始化,下面我們就來詳細介紹一下。

本篇文章來介紹一道非常常見的面試題,到底有多常見呢?可能很多面試的開場白就是由此開始的。那就是 new 和 make 這兩個內置函數的區(qū)別。

其實這個問題本身并不復雜,簡單來說就是,new 只分配內存,而 make 只能用于 slice、map 和 chan 的初始化,下面我們就來詳細介紹一下。

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

從上面的代碼可以看出,new 函數只接受一個參數,這個參數是一個類型,并且返回一個指向該類型內存地址的指針。

同時 new 函數會把分配的內存置為零,也就是類型的零值。

使用

使用 new 函數為變量分配內存空間:

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

上面的代碼是等價的,new(int) 將分配的空間初始化為 int 的零值,也就是 0,并返回 int 的指針,這和直接聲明指針并初始化的效果是相同的。

當然,new 函數不僅能夠為系統(tǒng)默認的數據類型分配空間,自定義類型也可以使用 new 函數來分配空間,如下所示:

type Student struct {
name string
age int
}
var s *Student
s = new(Student) //分配空間
s.name = "zhangsan"
fmt.Println(s)

這就是 new 函數,它返回的永遠是類型的指針,指針指向分配類型的內存地址。需要注意的是,new 函數只會分配內存空間,但并不會初始化該內存空間。

make

make 也是用于內存分配的,但是和 new 不同,它只用于 slice、map 和 chan 的內存創(chuàng)建,而且它返回的類型就是這三個類型本身,而不是他們的指針類型。因為這三種類型本身就是引用類型,所以就沒有必要返回他們的指針了。

其函數簽名如下:

源碼

// 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 參數必須是 slice、map 和 chan 中的一個,并且返回值也是類型本身。

使用

下面用 slice 來舉一個例子:

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 內容被類型 int 的零值填充,如:[]int{0, 0, 0}。

map 和 chan 也是類似的,就不多說了。

總結

通過以上分析,總結一下 new 和 make 主要區(qū)別如下:

  1. make 只能用來分配及初始化類型為 slice、map 和 chan 的數據。new 可以分配任意類型的數據;
  2. new 分配返回的是指針,即類型 *Type?。make 返回類型本身,即 Type;
  3. new 分配的空間被清零。make 分配空間后,會進行初始化;

以上就是本文的全部內容,如果覺得還不錯的話歡迎點贊,轉發(fā)和關注,感謝支持。

參考文章:

  • 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/
責任編輯:武曉燕 來源: AlwaysBeta
相關推薦

2021-08-06 07:51:47

關鍵字int函數

2023-10-23 19:27:21

Go函數

2009-08-21 14:16:35

C# New關鍵字

2010-03-08 08:39:54

類加載器newJava

2022-01-10 18:11:42

C語言應用技巧

2015-11-10 16:10:22

C語言StaticConst

2024-06-04 17:02:38

newC#編程語言

2017-10-20 11:12:12

數據類型關鍵字對象

2010-01-15 15:45:10

VB.NET New關

2023-10-27 11:27:14

Go函數

2009-06-29 12:58:47

This關鍵字java

2024-04-01 00:00:00

??defer??工具管理

2021-12-07 08:55:55

Go InterfaceAny

2011-05-17 13:04:20

Cconst

2021-03-05 08:51:00

Go語言make

2024-01-07 20:15:49

Python編程語言

2024-12-31 00:05:24

new?關鍵字C#

2024-03-15 15:12:27

關鍵字底層代碼

2022-11-12 18:32:50

Golangomitemptyjson

2021-02-01 13:10:07

Staticc語言UNIX系統(tǒng)
點贊
收藏

51CTO技術棧公眾號