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()适用于所有数据类型(通道,映射除外),并为该类型的变量动态分配空间,并将其初始化为该类型的零值并返回指向它的指针。
Example:
例:
result = new(int)
is equivalent to
相当于
var temp int // declare an int type variable
var result *int // declare a pointer to int
result = &temp
Example/program:
示例/程序:
There are three different ways to create a pointer that points to a zeroed structure value, each of which is equivalent:
有三种不同的方法可以创建指向零结构值的指针,每种方法都等效:
package main
import "fmt"
type Sum struct {
x_val int
y_val int
}
func main() {
// Allocate enough memory to store a Sum structure value
// and return a pointer to the value's address
var sum Sum
p := &sum
fmt.Println(p)
// Use a composite literal to perform
//allocation and return a pointer
// to the value's address
p = &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 main
import "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创建切片,并根据默认数据类型值初始化其内容。 这里使用int,所以默认值为0。
new([20] int)[0:10] –在这里,它还将创建切片,但返回指向已初始化内存的指针。
示例/程序:
有两种不同的初始化映射的方法,将字符串键映射到bool值的方法如下。
package main
import "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
最佳面试编码问题/挑战
游程编码(字符串中字母的查找/打印频率)
以线性时间复杂度对0、1和2的数组进行排序
检查字谜(检查两个字符串是否是字谜)
相对排序算法
查找给定总和的子数组
在给定总和K的二叉树中找到级别
检查二叉树是否为BST(二叉搜索树)
1 [0] 1个样式计数
大写一行中每个单词的第一个和最后一个字母
打印二叉树的垂直和
打印二叉树的边界和
反转单个链表
解决主要算法问题的贪婪策略
工作排序问题
根到叶的路径总和
矩阵中的出口点
在链表中查找循环长度
一流的礼帽
打印所有没有兄弟的节点
转换为求和树
最短的源到目标路径
评论和讨论
广告:您是博主吗? 加入我们的Blogging论坛 。
请启用JavaScript以查看由Disqus提供的评论。
翻译自: https://www.includehelp.com/golang/new-and-make-functions-with-examples.aspx
golang的new函数
golang的new函数_new()和make()函数以及Golang中的示例相关推荐
- matlab中now函数_now()方法以及JavaScript中的示例
matlab中now函数 JavaScript now()方法 (JavaScript now() method) now() method is a Date class method, it is ...
- golang 切片 接口_Golang简单入门教程——函数进阶使用
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是golang专题的第八篇,我们来聊聊golang当中的函数. 我们在之前的时候已经介绍过了函数的基本用法,知道了怎么样设计或者是定义一 ...
- Golang:简介、基本语法、函数、defer、Test功能
春招找实习告一段落了,好长时间没更CSDN的博客,期间写的一些笔记用 typora + git 直接推到github里面了,就没在CSDN里再发了,我的github:https://github.co ...
- Golang——秒懂函数、参数、可变参数、匿名函数、回调函数、内置函数
函数: 函数是结构化编程的最小模块单元.它将复杂的算法过程分解为若干较小任务,隐藏相关 细节,使得程序结构更加清晰,易于维护.函数被设计成相对独立,通过接收输入参数完成一段算法指令,输出或存储相关结果 ...
- go函数详解:函数定义、形参、返回值定义规范、函数内存分析、不支持重载、支持可变参数、基本数据类型和数组默认都是值传递的、支持自定义数据类型、函数返回值命名
引入 [1]为什么要使用函数: 提高代码的复用型,减少代码的冗余,代码的维护性也提高了 [2]函数的定义: 为完成某一功能的程序指令(语句)的集合,称为函数. [3]基本语法 func 函数名(形参列 ...
- c/c++中的函数指针和指针函数
定义 1.指针函数,本质是函数,返回值为指针,形如,int *pfun(int, int),由于"*"的优先级低于"()"的优先级,所以等同于int *(pfu ...
- C++ 虚函数与存虚函数
什么是虚函数: 虚函数 是在基类中使用关键字 virtual 声明的函数,在C++ 语言中虚函数可以继承,当一个成员函数被声明为虚函数之后,其派生类中的同名函数都自动生成为虚函数, 虚函数主要体验C+ ...
- Go 学习笔记(16)— 函数(02)[函数签名、有名函数、匿名函数、调用匿名函数、匿名函数赋值给变量、匿名函数做回调函数]
1. 函数签名 函数类型也叫做函数签名,可以使用 fmt.Printf("%T") 格式化参数打印函数类型. package mainimport "fmt"f ...
- Go 学习笔记(15)— 函数(01)[函数定义、函数特点、多值返回、实参形参、变长参数,函数作为参数调用]
1. 函数定义 Go 语言最少有个 main() 函数.函数声明告诉了编译器函数的名称,返回类型和参数. func funcName(parameter_list)(result_list) {fun ...
- php 回调递归,PHP数组函数 array_walk_recursive (使用回调函数递归遍历数组元素)
在PHP中,数组函数 array_walk_recursive () 使用回调函数递归遍历数组元素. 函数语法:array_walk_recursive ( array &$array , c ...
最新文章
- 报名 | 加密金融生态大咖私享会
- Digg 让 八 动起来,让 八 产生价值
- vue中v-for循环如何将变量带入class的属性名中
- 七、前端开发-JavaScript HTTP
- Applese 走迷宫(优先队列+bfs)
- ASP.NET Core Linux下为 dotnet 创建守护进程(必备知识)
- react学习(49)--参数判定
- ExtJS表单提交与加载全攻略
- Silverlight Code Samples
- python selenium安装
- android 数据存储之 读写文件
- Axure 8.0/9.0 注册码 激活码 授权码 License
- Altium Designer精简版
- 英语口语收集(十九)
- 躲在被窝里偷偷学爬虫(6)---处理cookie模拟登录及代理IP
- Virtual Box报错VT-x is not available (VERR_VMX_NO_VMX)--大踩坑(二)
- SpringBoot+Vue+Mybatis-plus 博客(一):完成博客后台前端登录页面、后端登录接口
- iCloud照片在哪看?如何查看iCloud里的照片
- [hdu-1814] Peaceful Commission题解
- 【SIM】MCC(移动国家码)和 MNC(移动网络码)