日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

Go語言中有自動垃圾回收的機制(garbage collection),不需要為內存回收擔心。而內存分配的有兩種操作方式,new和make,本節重點講述這兩種操作方式。

new

內置函數new與其他語言類似,new(T)為T類型分配一個零空間,并返回其地址,即*T類型的值。或者換句話說,它返回一個指向新分配的T類型零值的指針,這一點需要牢記。

bytes.Buffer 的文檔指出“Buffer 的零值是一個可以使用的空緩沖區”。 同樣,sync.Mutex沒有顯式構造函數或Init方法。 相反,sync.Mutex 的零值被定義為未鎖定的互斥鎖。

參數定義

func new(Type) *Type

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.

make

內置的make(T)僅用于創建slices, maps和channels,返回類型T的初始化值(非零!),并且不是一個指針*T。產生這種區別的原因,是在使用前引用的數據類型必須進行初始化。

參數定義

func make(t Type, size ...IntegerType) Type

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.

區別

new只負責分配,make負責初始化,區別點:

  • new(T)返回指針,*T指向零值T
  • make(T)返回初始化后的T

分享到:
標簽:語言
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定