golang的new函数_new()和make()函数以及Golang中的示例
golang的new函數
In Golang, to allocate memory, we have two built-in functions new() and make().
在Golang中,要分配內存,我們有兩個內置函數new()和make() 。
1)new()函數 (1) new() function)
Memory returned by new() is zeroed.
new()返回的內存為零。
new() only returns pointers to initialized memory.
new()僅返回指向初始化內存的指針。
new() works for all the data types (except channel, map), and dynamically allocates space for a variable of that type and initialized it to zero value of that type and return a pointer to it.
new()適用于所有數據類型(通道,映射除外),并為該類型的變量動態(tài)分配空間,并將其初始化為該類型的零值并返回指向它的指針。
Example:
例:
result = new(int)is equivalent to
相當于
var temp int // declare an int type variablevar result *int // declare a pointer to intresult = &tempExample/program:
示例/程序:
There are three different ways to create a pointer that points to a zeroed structure value, each of which is equivalent:
有三種不同的方法可以創(chuàng)建指向零結構值的指針,每種方法都等效:
package mainimport "fmt"type Sum struct {x_val inty_val int }func main() {// Allocate enough memory to store a Sum structure value// and return a pointer to the value's addressvar sum Sump := &sumfmt.Println(p)// Use a composite literal to perform //allocation and return a pointer// to the value's addressp = &Sum{}fmt.Println(p)// Use the new function to perform allocation, //which will return a pointer to the value's address.p = new(Sum)fmt.Println(p) }Output
輸出量
&{0 0} &{0 0} &{0 0}2)make()函數 (2) make() function)
make() only makes slices, maps, and channels. make returns value of type T(data type) not *T
Example of slices:
make([]int, 10, 20) – Here, make creates the slice, and initialize its content depending on the default data type value. here int is used, so the default value is 0.
new([20]int)[0:10] – Here, It will also create slice but returns pointers to initialized memory.
Example/program:
There are two different ways to initialize a map which maps string keys to bool values are given below.
package mainimport "fmt"func main() {// Using make() to initialize a map.m := make(map[string]bool, 0)fmt.Println(m)// Using a composite literal to initialize a map.m = map[string]bool{}fmt.Println(m) }Output
Reference: allocation_new
TOP Interview Coding Problems/Challenges
Run-length encoding (find/print frequency of letters in a string)
Sort an array of 0's, 1's and 2's in linear time complexity
Checking Anagrams (check whether two string is anagrams or not)
Relative sorting algorithm
Finding subarray with given sum
Find the level in a binary tree with given sum K
Check whether a Binary Tree is BST (Binary Search Tree) or not
1[0]1 Pattern Count
Capitalize first and last letter of each word in a line
Print vertical sum of a binary tree
Print Boundary Sum of a Binary Tree
Reverse a single linked list
Greedy Strategy to solve major algorithm problems
Job sequencing problem
Root to leaf Path Sum
Exit Point in a Matrix
Find length of loop in a linked list
Toppers of Class
Print All Nodes that don't have Sibling
Transform to Sum Tree
Shortest Source to Destination Path
Comments and Discussions
Ad: Are you a blogger? Join our Blogging forum.
Please enable JavaScript to view the comments powered by Disqus.
make()僅制作切片,地圖和通道。 make返回類型T (數據類型)的值,而不是* T
切片示例:
make([] int,10,20) –在這里,make創(chuàng)建切片,并根據默認數據類型值初始化其內容。 這里使用int,所以默認值為0。
new([20] int)[0:10] –在這里,它還將創(chuàng)建切片,但返回指向已初始化內存的指針。
示例/程序:
有兩種不同的初始化映射的方法,將字符串鍵映射到bool值的方法如下。
package mainimport "fmt"func main() {// Using make() to initialize a map.m := make ( map [ string ] bool , 0 )fmt.Println(m)// Using a composite literal to initialize a map.m = map [ string ] bool {}fmt.Println(m) }輸出量
參考: allocation_new
最佳面試編碼問題/挑戰(zhàn)
游程編碼(字符串中字母的查找/打印頻率)
以線性時間復雜度對0、1和2的數組進行排序
檢查字謎(檢查兩個字符串是否是字謎)
相對排序算法
查找給定總和的子數組
在給定總和K的二叉樹中找到級別
檢查二叉樹是否為BST(二叉搜索樹)
1 [0] 1個樣式計數
大寫一行中每個單詞的第一個和最后一個字母
打印二叉樹的垂直和
打印二叉樹的邊界和
反轉單個鏈表
解決主要算法問題的貪婪策略
工作排序問題
根到葉的路徑總和
矩陣中的出口點
在鏈表中查找循環(huán)長度
一流的禮帽
打印所有沒有兄弟的節(jié)點
轉換為求和樹
最短的源到目標路徑
評論和討論
廣告:您是博主嗎? 加入我們的Blogging論壇 。
請啟用JavaScript以查看由Disqus提供的評論。
翻譯自: https://www.includehelp.com/golang/new-and-make-functions-with-examples.aspx
golang的new函數
總結
以上是生活随笔為你收集整理的golang的new函数_new()和make()函数以及Golang中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 16位的数字高字节和低字节_显示掩盖较低
- 下一篇: ascii非打印控制字符表_C程序打印A