新余集团网站建设,塘厦企业网站推广公司,wordpress显示空白,网站网站怎么做的文章目录 前言new源码使用 make源码使用 总结 前言
本篇文章来介绍一道非常常见的面试题#xff0c;到底有多常见呢#xff1f;可能很多面试的开场白就是由此开始的。那就是 new 和 make 这两个内置函数的区别。
在 Go 语言中#xff0c;有两个比较雷同的内置函数#xf… 文章目录 前言new源码使用 make源码使用 总结 前言
本篇文章来介绍一道非常常见的面试题到底有多常见呢可能很多面试的开场白就是由此开始的。那就是 new 和 make 这两个内置函数的区别。
在 Go 语言中有两个比较雷同的内置函数分别是 new 和 make 方法其主要用途都是用于分配相应类型的内存空间。 看上去 new 和 make 都是分配内存的那他们有什么区别呢这个细节点也成为了不少 Go 语言工程师的面试题之一值得大家一看。
其实这个问题本身并不复杂简单来说就是new 只分配内存而 make 只能用于 slice、map 和 chan 的初始化下面我们就来详细介绍一下。
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 函数只接受一个参数这个参数是一个类型并且返回一个指向该类型内存地址的指针。 同时 new 函数会把分配的内存置为零也就是类型的零值。
使用
使用 new 函数为变量分配内存空间
p1 : new(int)
fmt.Printf(p1 -- %#v \n , p1) //(*int)(0xc42000e250)
fmt.Printf(p1 point to -- %#v \n , *p1) //0var p2 *int
i : 0
p2 i
fmt.Printf(p2 -- %#v \n , p2) //(*int)(0xc42000e278)
fmt.Printf(p2 point to -- %#v \n , *p2) //0上面的代码是等价的new(int) 将分配的空间初始化为 int 的零值也就是 0并返回 int 的指针这和直接声明指针并初始化的效果是相同的。
当然new 函数不仅能够为系统默认的数据类型分配空间自定义类型也可以使用 new 函数来分配空间如下所示
type Student struct {name stringage int
}
var s *Student
s new(Student) //分配空间
s.name zhangsan
fmt.Println(s)这就是 new 函数它返回的永远是类型的指针指针指向分配类型的内存地址。需要注意的是new 函数只会分配内存空间但并不会初始化该内存空间。
make
make 也是用于内存分配的但是和 new 不同它只用于 slice、map 和 chan 的内存创建而且它返回的类型就是这三个类型本身而不是他们的指针类型。因为这三种类型本身就是引用类型所以就没有必要返回他们的指针了。 其函数签名如下
源码
// 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, makes 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 channels buffer is initialized with the specified
// buffer capacity. If zero, or the size is omitted, the channel is
// unbuffered.
func make(t Type, size ...IntegerType) Type通过上面的代码可以看出 make 函数的 t 参数必须是 slice、map 和 chan 中的一个并且返回值也是类型本身。
使用
下面用 slice 来举一个例子
var s1 []int
if s1 nil {fmt.Printf(s1 is nil -- %#v \n , s1) // []int(nil)
}s2 : make([]int, 3)
if s2 nil {fmt.Printf(s2 is nil -- %#v \n , s2)
} else {fmt.Printf(s2 is not nill -- %#v \n , s2)// []int{0, 0, 0}
}slice 的零值是 nil但使用 make 初始化之后slice 内容被类型 int 的零值填充如[]int{0, 0, 0}。 map 和 chan 也是类似的就不多说了。
总结
通过以上分析总结一下 new 和 make 主要区别如下
make 只能用来分配及初始化类型为 slice、map 和 chan 的数据。new 可以分配任意类型的数据new 分配返回的是指针即类型 *Type。make 返回类型本身即 Typenew 分配的空间被清零。make 分配空间后会进行初始化 文章转载自: http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.rdkgw.cn.gov.cn.rdkgw.cn http://www.morning.bklhx.cn.gov.cn.bklhx.cn http://www.morning.a3e2r.com.gov.cn.a3e2r.com http://www.morning.wkrkb.cn.gov.cn.wkrkb.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.linzhigongmao.cn.gov.cn.linzhigongmao.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.rqlzz.cn.gov.cn.rqlzz.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.qmzhy.cn.gov.cn.qmzhy.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.xkwrb.cn.gov.cn.xkwrb.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.rpdmj.cn.gov.cn.rpdmj.cn http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.mxhgy.cn.gov.cn.mxhgy.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.rwyw.cn.gov.cn.rwyw.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.ghlyy.cn.gov.cn.ghlyy.cn http://www.morning.rfgc.cn.gov.cn.rfgc.cn http://www.morning.fmqng.cn.gov.cn.fmqng.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.dkmzr.cn.gov.cn.dkmzr.cn http://www.morning.ymwnc.cn.gov.cn.ymwnc.cn http://www.morning.nstml.cn.gov.cn.nstml.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.hnkkm.cn.gov.cn.hnkkm.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.tzmjc.cn.gov.cn.tzmjc.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.dycbp.cn.gov.cn.dycbp.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.dqzcf.cn.gov.cn.dqzcf.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.zlnyk.cn.gov.cn.zlnyk.cn