成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Golang面試make和new的用法

開發 前端
在golang中,make和new都分配內存,但是它們之間仍然存在一些差異。只有了解它們之間的差異,才能在適當的場合使用它們。

在golang中,make和new都分配內存,但是它們之間仍然存在一些差異。只有了解它們之間的差異,才能在適當的場合使用它們。

簡而言之,new只是分配內存,而不初始化內存;make分配并初始化內存。所謂的初始化就是給一個類型賦一個初始值,例如,字符為空,整數為0,邏輯值為false。

從Golang的官方文檔的builtin(內置的軟件包)中可以找到,make和new的用法。

 

Golang面試make和new的用法
官方文檔的內置的軟件包

new的使用介紹:

我們先來看看new的定義

  1. // The new built-in function allocates memory. The first argument is a type, 
  2. // not a value, and the value returned is a pointer to a newly 
  3. // allocated zero value of that type. 
  4. //內建函數new分配內存。其第一個實參為類型,而非值。其返回值為指向該類型的新分配的零值的指針。 
  5. func new(Type) *Type 

可以看出,它的參數是一個類型,返回值是指向該類型的內存地址的指針,并且分配的內存將被設置為零,即該類型的零值,即字符為空,整數為0,邏輯值為false

看一些例子

  1. type P struct { 
  2.         Name string 
  3.         Age  int 
  4.     }   var a *[2]int 
  5.     var s *string 
  6.     var b *bool 
  7.     var i *int 
  8.     var ps *P 
  9.     a = new([2]int
  10.     s = new(string) 
  11.     b = new(bool) 
  12.     i = new(int
  13.     ps = new(P) //structure 
  14.     fmt.Println(a, " ", *a) 
  15.     fmt.Println(s, " ", *s) 
  16.     fmt.Println(b, " ", *b) 
  17.     fmt.Println(i, " ", *i) 
  18.     fmt.Println(ps, " ", *ps) 

輸出如下:

  1. &[0 0]   [0 0] 
  2. 0xc0000821e0    
  3. 0xc0000a409a   false 
  4. 0xc0000a40b0   0 
  5. &{ 0}   { 0} 

上面基礎類型,我們看一下slice, map and channel類型是如何操作的:

  1. //map 操作 
  2.    var mp *map[string]string 
  3.    mp = new(map[string]string) 
  4.     //注釋掉下面的行,new map 返回為nil,直接使用會panic 
  5.    //*mp = make(map[string]string) // if this line is omitted, it will pan "Pan: assignment to entry in nil map"“ 
  6.    (*mp)["name"] = "lc" 
  7.    fmt.Println((*mp)["name"]) 
  8.       // slice 操作    var ms *[]string 
  9.    ms = new([]string) 
  10.   // 注釋掉下面的行訪問的時候會下標超出范圍    //*ms = make([]string,5) // if this line is deleted, it will "panic: runtime error: index out of range" 
  11.    (*ms)[0] = "lc" 
  12.    fmt.Println((*ms)[0])  

從上面可以看出,silce,map,channel和其他類型是引用類型。當引用類型初始化為nil時,不能直接分配nil,也不能使用new來分配內存,還需要使用make來進行分配。

make的使用介紹:

我們看一下make的定義

  1. / /The make built-in function allocates and initializes an object of type 
  2. // slice, map, or chan (only). Like new, the first argument is a type, not a 
  3. // value. Unlike new, make's return type is the same as the type of its 
  4. // argument, not a pointer to it. The specification of the result depends on 
  5. // the type: 
  6. //  Slice: The size specifies the length. The capacity of the slice is 
  7. //  equal to its length. A second integer argument may be provided to 
  8. //  specify a different capacity; it must be no smaller than the 
  9. //  length. For example, make([]int, 0, 10) allocates an underlying array 
  10. //  of size 10 and returns a slice of length 0 and capacity 10 that is 
  11. //  backed by this underlying array. 
  12. //  Map: An empty map is allocated with enough space to hold the 
  13. //  specified number of elements. The size may be omitted, in which case 
  14. //  a small starting size is allocated. 
  15. //  Channel: The channel's buffer is initialized with the specified 
  16. //  buffer capacity. If zero, or the size is omitted, the channel is 
  17. //  unbuffered. 
  18. //切片:size指定了其長度。該切片的容量等于其長度。切片支持第二個整數實參可用來指定不同的容量; 它必須不小于其長度,因此 make([]int, 0, 10) 會分配一個長度為0,容量為10的切片。 
  19. //映射:初始分配的創建取決于size,但產生的映射長度為0。size可以省略,這種情況下就會分配一個小的起始大小。 
  20. //通道:通道的緩存根據指定的緩存容量初始化。若 size為零或被省略,該信道即為無緩存的。 
  21. func make(t Type, size ...IntegerType) Type 

可以看出,它返回的是類型本身而不是指針類型,因為make只能為slice,map,channel等初始化內存,并且它們返回引用類型,因此不必返回指針

讓我們看一些make的例子:

  1. mm :=make(map[string]string) 
  2.   mm["name"] = "lc" 
  3.   fmt.Println(mm["name"]) 
  4.   mss :=make([]int,2) 
  5.   mss[0] = 100 
  6.   fmt.Println(mss[0]) 
  7.   ch :=make(chan int,1) 
  8.   ch <-100 
  9.   fmt.Println(<-ch) 

總結:

make僅用于分配和初始化slice,map和chan類型的數據。new可以分配任何類型的數據。new分配返回一個指針,即Type * Type。make返回一個引用,該引用為Type由make分配的空間之后,清除并初始化由new分配的空間。

責任編輯:未麗燕 來源: 今日頭條
相關推薦

2022-10-24 00:03:26

GolangNew函數

2021-03-05 08:51:00

Go語言make

2023-10-23 19:27:21

Go函數

2023-03-24 08:01:27

Go語言內存

2023-10-27 11:27:14

Go函數

2024-09-10 08:49:52

Go語言內存

2022-07-07 06:27:59

Python__init____new__

2021-09-22 12:56:19

編程技能Golang

2024-06-04 17:02:38

newC#編程語言

2023-10-24 16:03:34

GoGolang

2021-12-27 03:36:09

語言For Golang

2010-02-01 15:18:40

C++ new用法

2022-04-11 07:40:45

synchroniz靜態方法程序

2025-04-03 09:12:26

GolangWaitGroup工具

2023-09-13 08:00:57

云原生Java開發者

2021-11-27 08:13:13

Final 面試

2024-12-06 12:09:56

Java場景Break

2024-12-31 00:05:24

new?關鍵字C#

2009-08-21 14:22:22

C# new和over

2022-07-27 08:27:34

Call前端
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久lu| 华丽的挑战在线观看 | 国产乱码精品1区2区3区 | 日本亚洲一区 | 日韩欧美一区在线 | 91视频网 | 国产九一精品 | 中文字幕一区二区三区精彩视频 | 国产欧美精品一区二区 | 国产精品免费一区二区三区 | 嫩草影院网址 | 国产高清视频 | 亚洲国产中文字幕 | 91国在线高清视频 | 欧美一区二区三区久久精品视 | 亚洲欧美中文字幕在线观看 | 网黄在线| 国产精品成人品 | av一级久久 | 美女逼网站 | 91五月天 | 欧美日韩视频在线第一区 | 99精品99久久久久久宅男 | 亚洲精品中文在线 | 久久久久久国产精品免费免费狐狸 | 精品一二三区在线观看 | 亚洲成人免费视频 | 日韩一区二区免费视频 | 国产精品178页 | 久久久久综合 | 国产成人免费在线 | 欧美黄色片 | 一二区视频| 国产精品久久国产精品 | 91影院在线观看 | 亚洲中字在线 | 人人九九精 | 国产视频二区在线观看 | 中国大陆高清aⅴ毛片 | 国产精品久久久久久久久久 | 黄色网址在线免费观看 |