Golang 中new 和 make 函数有什么不同

2023-06-1017:44:44编程语言入门到精通Comments619 views字数 1897阅读模式

Golang 中 new() 函数是另外一种创建变量的方式,内建的 new(T) 函数为一个 T 类型的新项分配了 "零 "存储,并返回其地址,即 *T 类型的值。用 Go 的术语来说,它返回一个指向新分配的 T 类型的零值的指针。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

Golang 中new 和 make 函数有什么不同

Golang new() 函数

函数语法:func new(Type) *Type文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

  • new() 返回的内存是清零的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html
  • new() 只返回指向初始化内存的指针。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html
  • new() 适用于所有的数据类型(除了 channelmap),并为该类型的变量动态分配空间,将其初始化为该类型的零值并返回一个指针。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

语法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

result = new(int)

这个语法等同于:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

var temp int   // declare an int type variable
var result *int //  declare a pointer to int
result = &temp 

有三种不同的方法来创建一个指向零结构值的指针,每种方法都是等价的:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

package main

import "fmt"

type Sum struct {
 x_val int
 y_val int
}

func main() {
 // Allocate enough memory to store a Sum structure value
 // and return a pointer to the value's address
 var sum Sum
 p := &sum
 fmt.Println(p)

 // Use a composite literal to perform 
 //allocation and return a pointer
 // to the value's address
 p = &Sum{}
 fmt.Println(p)

 // Use the new function to perform allocation, 
 //which will return a pointer to the value's address.
 p = new(Sum)
 fmt.Println(p)
}

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

&{0 0}
&{0 0}
&{0 0}

使用 new() 创建一个变量和先通过 var 初始化一个变量,然后对这个变量取地址没什么不同,唯一的区别是,通过 new() 函数不需要引入变量名称,所以使用上更加简洁、便利。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

Golang make() 函数

函数语法:func make(t Type, size ...IntegerType) Type,和new一样,第一个参数是一个类型,而不是一个值。与new不同,make的返回类型与它的参数类型相同,而不是一个指向它的指针。结果的规格取决于类型。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

make() 函数是一种特殊的内置函数,只用于初始化切片、map 和通道。make 返回 T (数据类型)类型的值,而不是 *T。与 new() 函数不同,make() 不会返回一个指针。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

make([]int, 10, 20) : make 创建了切片,并根据默认的数据类型值初始化其内容。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

new([20]int)[0:10] : 它也将创建片断,但返回指向初始化内存的指针。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

  • 切片:尺寸指定的是长度。分片的容量是等于它的长度。可以提供第二个整数参数来指定不同的容量。指定一个不同的容量;它必须不小于长度。例如,make([]int, 0, 10) 分配了一个底层数组大小为 10 的底层数组,并返回一个长度为 0、容量为 10 的切片,该切片由底层数组支持。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html
  • map:一个空的 map 被分配了足够的空间来容纳指定数量的元素。大小可以省略,在这种情况下文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

会分配一个小的起始大小。make(map[int]bool, 10)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

  • 通道:通道的缓冲区被初始化为指定的缓冲区容量。如果是零,或者省略了大小,通道就会被没有缓冲区。make(chan int, 5)
package main

import "fmt"

func main() {
 // Using make() to initialize a map.
 m := make(map[string]bool, 0)
 fmt.Println(m)

 // Using a composite literal to initialize a map.
 m = map[string]bool{}
 fmt.Println(m)
}

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

map[]
map[]

你也可以使用一个复合字面的初始数据来初始化 map,如下图所示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html

m := map[string]bool{
    "java": false,
    "go":   true,
}
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46833.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/46833.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定