• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Go语言学习之make和new的区别

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.new函数

在官方文档中,new函数的描述如下

// 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.
func new(Type) *Type

可以看到new函数只能传递一个参数,该参数为一个任意类型,可以是go的内建类型,也可以是你自定义的类型

那么new函数到底做了哪些事呢.

  • 分配内存
  • 设置零值
  • 返回指针(重要)

举个例子

import "fmt"

type Student struct {
   name string
   age int
}

func main() {
    // new 一个内建类型
    num := new(int)
    fmt.Println(*num) //打印零值:0

    // new 一个自定义类型
    s := new(Student)
    s.name = "zhangsan"
}

 

2.make函数

在官方文档中,make函数描述如下

//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.
func make(t Type, size …IntegerType) Type

翻译:

  • 1.内建函数 make 用来为 slice,map 或 chan 类型(注意:也只能用在这三种类型上)分配内存和初始化一个对象
  • 2.make 返回类型的本身而不是指针,而返回值也依赖于具体传入的类型,因为这三种类型(slice,map 和 chan)本身就是引用类型,所以就没有必要返回他们的指针了

由于这三种类型都是引用类型,所以必须得初始化(size和cap),但是不是置为零值,这个和new是不一样的

//切片
a := make([]int, 2, 10)  

// 字典
b := make(map[string]int)

// 通道
c := make(chan int, 10)

3.总结

new:为所有的类型分配内存,并初始化为零值,返回指针。

make:只能为 slice,map,chan 分配内存,并初始化,返回的是类型。

另外,目前来看 new 函数并不常用,大家更喜欢使用短语句声明的方式

a := new(int)
a = 1
// 等价于
a := 1

但是 make 就不一样了,它的地位无可替代,在使用slice、map以及channel的时候,还是要使用make进行初始化,然后才可以对他们进行操作

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go struct{}的几种特殊用法发布时间:2022-07-10
下一篇:
golang 汇编 GO汇编-函数发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap