5

Go make和new的区别是什么?

 2 years ago
source link: https://studygolang.com/articles/35789
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Go make和new的区别是什么?

xiangbei · 6天之前 · 696 次点击 · 预计阅读时间不到 1 分钟 · 大约8小时之前 开始浏览    

make
make也是用于内存分配的,区别于new,它只用于slice、map、chan的内存创建
而且它返回的类型就是这三个类型本身,而不是他们的指针类型
因为这三种类型就是引用类型,所以就没有必要返回他们的指针了

make和new的区别

  • 1.make和new都是用来申请内存的
  • 2.new很少用,一般用来给基本数据类型申请内存,string、int等,返回的是对应类型的指针*string、*int
  • 3.make用于给slice、map、chan申请内存,make函数的返回的是这三个类型本身

http://www.1024ask.com/q/168954179878912000


有疑问加站长微信联系(非本文作者)

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK