Go语言中new和make的区别
Go语言中new跟make是内置函数,主要用来创建分配类型内存。
new( )
new(T)创建一个没有任何数据的类型为T的实例,并返回该实例的指针;
源码解析
func new
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, args)只能创建 slice、map和channel,并且返回一个有初始值args(非零)的T类型的实例,非指针。
源码解析
func make
func make(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, 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.
二者异同
二者都是内存的分配(堆上),但是make只用于slice、map以及channel的初始化(非零值);而new用于类型的内存分配,并且内存置为零。所以在我们编写程序的时候,就可以根据自己的需要很好的选择了。
make返回的还是这三个引用类型本身;而new返回的是指向类型的指针。
作者:子恒|haley
出处:http://www.haleyl.com
交流沟通:QQ群866437035
Go语言中new和make的区别的更多相关文章
- 浅谈Java语言中ArrayList和HashSet的区别
Java语言中ArrayList和HashSet的区别 2019-04-10 13:22:49 一.基本区别 首先一起看个实例,其代码如下: package com.MrZ_baby.com; i ...
- 面试题----C语言中exit和return的区别
C语言中return和exit的区别 exit用于结束进程,返回的状态码是给操作系统使用或父进程使用的.return是堆栈返回,返回的值是给主调函数用的.主线程结束前会默认调用exit结束进程. ex ...
- Go语言中的byte和rune区别、对比
Go语言中byte和rune实质上就是uint8和int32类型.byte用来强调数据是raw data,而不是数字:而rune用来表示Unicode的code point.参考规范: uint8 t ...
- c语言中%s和%d的区别
/************************************************************************* > File Name: ptr_both. ...
- Go语言中Goroutine与线程的区别
1.什么是Goroutine? Goroutine是建立在线程之上的轻量级的抽象.它允许我们以非常低的代价在同一个地址空间中并行地执行多个函数或者方法.相比于线程,它的创建和销毁的代价要小很多,并且它 ...
- Java编程语言中sleep()和yield()的区别
转自:http://developer.51cto.com/art/201003/189465.htm 1. Thread.yield(): api中解释: 暂停当前正在执行的线程对象,并执行 ...
- C语言中exit()与return的区别
整理自exit函数和return函数 1.exit函数和return函数的主要区别是: 1)exit用于在程序运行的过程中随时结束程序,exit的参数是返回给OS的.main函数结束时也会隐式地调用e ...
- C语言中sizeof与strlen的区别
1.sizeof sizeof为编译时期被替换,不会等到程序运行再来判断,所以sizeof返回的是数组的总字节数 #include<stdio.h> int main() { ]={'a' ...
- go语言中make和new的区别
make用于内建类型(map.slice 和channel)的内存分配.new用于各种类型的内存分配. 内建函数new本质上说跟其他语言中的同名函数功能一样:new(T)分配了零值填充的T类型的内存空 ...
随机推荐
- OpenCl入门getting-started-with-opencl-and-gpu-computing
原文来自于:getting-started-with-opencl-and-gpu-computing/ 对整个程序的注释:http://www.kimicat.com/opencl-1/opencl ...
- 使用nodejs实现OData的batch操作在Marketing Cloud里读取contact信息
我们先来看看Marketing Cloud系统里的contact信息: 一共1218374条数据. 我们用如下的nodejs代码通过OData来获取这些数据: var request = requir ...
- 使用FindCmdLineSwitch处理命令行参数
一.四个形式(变体) .function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet; IgnoreCase: B ...
- com.android.ddmlib.adbcommandrejectedexception:未经授权的设备。
出现这种问题的原因是adb被杀死了,根据网上的说法在platform-tools下双击adb.exe 也启动不了. 在命令提示符中执行 adb kill-server adb start-ser ...
- Linux基础篇之CentOS的网络配置(DHCP,静态)
1.启动系统,使用用户名.密码登录系统: 2. 配置网卡(DHCP获取IP地址.静态手动配置IP地址): 网卡的默认信息 DHCP模式修改为(下图): 静态IP地址修改为(下图): 无论哪种配置, ...
- PAT Basic 1057 数零壹 (20 分)
给定一串长度不超过 1 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有多少 0.多少 1.例如给定 ...
- java——spring中bean的作用域
文章:理解Spring框架中Bean的作用域 博客地址:https://baijiahao.baidu.com/s?id=1610298792072480906&wfr=spider& ...
- java——double数据精度问题
代码:使用BigDecimal来代替double public class BigDecimalUtil { public static BigDecimal add(double v1,double ...
- 认识Caffe与Caffe2
认识Caffe与Caffe2 目录: 一.Caffe的作者-贾扬清 二.Caffe简介--Caffe.Caffe2.Caffe2Go 三.认识Caffe 四.认识Caffe2 五.认识Caffe2Go ...
- Java8-Stream-No.04
import java.util.OptionalInt; import java.util.stream.IntStream; public class Streams4 { public stat ...
