当前位置: 首页 > news >正文

一般通过会社员全专业优化公司

一般通过会社员,全专业优化公司,代理网约车平台赚钱吗,静态网站添加到织梦文章目录 前言new源码使用 make源码使用 总结 前言 本篇文章来介绍一道非常常见的面试题,到底有多常见呢?可能很多面试的开场白就是由此开始的。那就是 new 和 make 这两个内置函数的区别。 在 Go 语言中,有两个比较雷同的内置函数&#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, 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.
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 返回类型本身,即 Type;
  • new 分配的空间被清零。make 分配空间后,会进行初始化;
http://www.bjxfkj.com.cn/article/105992.html

相关文章:

  • 免费域名网站创建怎样无货源开网店
  • 有网站如何做app最新热搜新闻
  • 电子商务网站建设与管理项目计划书广州网站优化软件
  • 如何做网站推广方法制作网站的软件叫什么
  • 顺德网站百度快照是怎么做上去的
  • 非洲购物网站排名网络营销概念
  • 中山做网站公司网络怎么做推广
  • 怎么做.com的网站东莞营销外包公司
  • wordpress 免费主题推荐最彻底的手机优化软件
  • 外贸建站模板免费seo是什么单位
  • 做内贸的网站口碑营销经典案例
  • 便宜网站建设 优帮云百度在线客服中心
  • 做脚本网站下载百度官方版
  • 一个公司只能备案一个网站吗app注册推广团队
  • DW做旅游网站毕业设计模板网络广告图片
  • 企业网站模板 下载 免费网络营销10大平台
  • 查询公司营业执照的网站创建网站花钱吗
  • 网站做一半能退吗seo搜索引擎优化软件
  • 沧州网站建设制作crm系统
  • 淮北做网站的公司大概需要多少钱
  • 做网站样式模板百度seo搜索营销新视角
  • 珠海动态网站制作外包重庆seo建站
  • 做直播网站vps可以吗宁波网站建设方案推广
  • 如何建设电影会员网站推广优化seo
  • 专业做网站开发的公司营销自动化
  • 创业型企业网站模板可以访问违规网站的浏览器
  • 天津葛沽 网站建设公司网站营销策划公司
  • 合肥手机网站制作营销策划是做什么
  • html网站搭建营销推广活动方案
  • 莱芜新闻最新事件seo优化标题 关键词