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的区别的更多相关文章

  1. 浅谈Java语言中ArrayList和HashSet的区别

    Java语言中ArrayList和HashSet的区别 2019-04-10   13:22:49 一.基本区别 首先一起看个实例,其代码如下: package com.MrZ_baby.com; i ...

  2. 面试题----C语言中exit和return的区别

    C语言中return和exit的区别 exit用于结束进程,返回的状态码是给操作系统使用或父进程使用的.return是堆栈返回,返回的值是给主调函数用的.主线程结束前会默认调用exit结束进程. ex ...

  3. Go语言中的byte和rune区别、对比

    Go语言中byte和rune实质上就是uint8和int32类型.byte用来强调数据是raw data,而不是数字:而rune用来表示Unicode的code point.参考规范: uint8 t ...

  4. c语言中%s和%d的区别

    /************************************************************************* > File Name: ptr_both. ...

  5. Go语言中Goroutine与线程的区别

    1.什么是Goroutine? Goroutine是建立在线程之上的轻量级的抽象.它允许我们以非常低的代价在同一个地址空间中并行地执行多个函数或者方法.相比于线程,它的创建和销毁的代价要小很多,并且它 ...

  6. Java编程语言中sleep()和yield()的区别

    转自:http://developer.51cto.com/art/201003/189465.htm 1. Thread.yield():     api中解释: 暂停当前正在执行的线程对象,并执行 ...

  7. C语言中exit()与return的区别

    整理自exit函数和return函数 1.exit函数和return函数的主要区别是: 1)exit用于在程序运行的过程中随时结束程序,exit的参数是返回给OS的.main函数结束时也会隐式地调用e ...

  8. C语言中sizeof与strlen的区别

    1.sizeof sizeof为编译时期被替换,不会等到程序运行再来判断,所以sizeof返回的是数组的总字节数 #include<stdio.h> int main() { ]={'a' ...

  9. go语言中make和new的区别

    make用于内建类型(map.slice 和channel)的内存分配.new用于各种类型的内存分配. 内建函数new本质上说跟其他语言中的同名函数功能一样:new(T)分配了零值填充的T类型的内存空 ...

随机推荐

  1. selenium无头浏览器&规避操作

    一.无头浏览器概述: 无头浏览器主要目的是打开浏览器但用户看不到 简单用法如下: from selenium import webdriver from time import sleep from ...

  2. 5.API详解

    Dao 中需要通过 SqlSession 对象来操作 DB.而 SqlSession 对象的创建, 需要其工厂对象 SqlSessionFactory.SqlSessionFactory 对象, 需要 ...

  3. 【Struts2】简介及入门

    一.概述 二.Struts2 快速入门程序 2.1 开发流程比较 2.2 引入依赖 2.2 创建jsp页面 2.3 在web.xml中配置前端控制器 2.4 创建struts.xml配置文件 2.4 ...

  4. Delphi 线程的基本概念

  5. 1.开始认识flask

    1. pip安装flask包pip install flask2.对flask最基本的使用from flask import Flask # 导入flask包 app = Flask(__name__ ...

  6. java——Servlet

    类要实现Servlet接口: 主要功能,生成动态网页内容: HttpServlet重写doGet和doPost方法或者重写Service方法,完成对请求的响应: 如:get.post等请求的响应. - ...

  7. bat 获取当前目录的父目录

    bat 获取当前目录的父目录 @echo off echo batchfile=%0 echo full=%~f0 setlocal for %%d in (%~dp0.) do set Direct ...

  8. hmmlearn 安装笔记

    hmmlearn是在python上实现隐马可夫模型的一个组件包,原先是在sklearn中的,后来被弃用而单独分离出来. 首先安装sklearn,最好下载setup.py的安装包用命令行安装,因为安装h ...

  9. 利用vue v-bind属性绑定bootstrap样式以及输出数据

    自从知道了bootstrap,就被他简介,大气美观的样式吸引,即使在vue框架中,仍旧想使用,下面给出了vue适配版和原版的代码,以飨读者 数据输出部分 export default { data() ...

  10. 利用python deque的extend特性实现列表元素查重

    from collections import deque mydquene = deque() mylist = [0,1,1,2,2,3,3,3,3,4,5,6,7,7,8,8,9,10,10,1 ...