<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Go 語言 new 和 make 關鍵字的區別

      原文鏈接: Go 語言 new 和 make 關鍵字的區別

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

      其實這個問題本身并不復雜,簡單來說就是,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 函數不僅能夠為系統默認的數據類型分配空間,自定義類型也可以使用 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 的內存創建,而且它返回的類型就是這三個類型本身,而不是他們的指針類型。因為這三種類型本身就是引用類型,所以就沒有必要返回他們的指針了。

      其函數簽名如下:

      源碼

      // 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 主要區別如下:

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

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


      參考文章:

      推薦閱讀:

      posted @ 2023-03-26 21:04  yongxinz  閱讀(243)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 思思久99久女女精品| 4hu44四虎www在线影院麻豆| 国产老熟女国语免费视频| 亚洲av无码精品色午夜| 国产精品无遮挡在线观看| 久久99精品久久久久久| 精品国产成人网站一区在线| 久爱无码精品免费视频在线观看| 欧美一区二区三区欧美日韩亚洲| 成人看的污污超级黄网站免费| 色老99久久精品偷偷鲁| 国产一区二区三区禁18| 亚洲成人四虎在线播放| 亚洲AV日韩精品久久久久| 熟妇女人妻丰满少妇中文字幕| 中文字幕亚洲人妻系列| 国产区成人精品视频| 亚洲欧洲一区二区精品| 在线欧美精品一区二区三区| 蜜臀av一区二区三区在线| 中文字幕免费不卡二区| 思思99热精品在线| av无码精品一区二区三区四区 | 成人综合婷婷国产精品久久蜜臀| 欧美自拍嘿咻内射在线观看| 日韩国产精品区一区二区| 国产线播放免费人成视频播放 | 巴塘县| 18禁极品一区二区三区| 无码日韩av一区二区三区| 少妇办公室好紧好爽再浪一点| 一二三四中文字幕日韩乱码| 永久免费精品性爱网站| 日韩va中文字幕无码电影| 中文字幕乱码在线人视频| 国产一区二区在线激情往| 精品av综合导航| 里番全彩爆乳女教师| 免费无码AV一区二区波多野结衣| 99欧美日本一区二区留学生| 天天爽天天摸天天碰|