Go 的 new 和 make


golang 变量定义的方式

可以通过 var+变量名称+变量类型 进行声明变量,当我们没有给它赋值的时候,它们的结果是变量类型的零值,例如:

  • string 的零值是””,
  • int 的零值是0,
  • 引用类型的零值是nil。

前面两种类型可以直接使用,但如果把它改成指针,就会报错

package main

import "fmt"

func main() {
  var str *string
  fmt.Println(str)
  *str = "hello world"
}

执行结果

<nil>
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10897d5]

goroutine 1 [running]:
main.main()
        /Users/xxxxx/go/src/github.com/xxx/go-demo/gin-demo/main.go:8 +0x55
exit status 2

因为对于引用类型的变量,不仅要声明,并且还要给它分配内存。分配内存就是使用 new

new

释义

new 分配内存的内置函数,第一个参数是类型,而不是具体的值,返回值是该类型的指针。分配的值是该类型零值的指针。
在这里插入图片描述

package main

import "fmt"

func main() {
  var str *string
  fmt.Println(str)
  str = new(string)
  *str = "hello world"
  fmt.Println(*str)
}

源码实现

底层调用 mallocgc 方法的时候,needzero 传的是 true ,返回值是传入类型零值的指针

func newobject(typ *_type) unsafe.Pointer{
	return mallocgc(typ.size,type,true);
}

make

释义

make 也是用于内存分配的内置函数
在这里插入图片描述

make内置函数分配并初始化一个slice、map或chan类型的对象。
像new函数一样,第一个参数是类型,而不是值。

与new不同,make的返回类型与其参数的类型相同,而不是指向它的指针。结果的取决于传入的类型。
并且 slice在 make 的时候,第二个参数必须传递,也就是切片的长度。否则会编译失败。

源码实现

不同的结构使用的方法不同

// slice
func makeslice(typ *byte, len int, cap int) unsafe.Pointer
func makeslice64(typ *byte, len int64, cap int64) unsafe.Pointer

// map
func makemap64(mapType *byte, hint int64, mapbuf *any) (hmap map[any]any)
func makemap(mapType *byte, hint int, mapbuf *any) (hmap map[any]any)
func makemap_small() (hmap map[any]any)

// chan
func makechan64(chanType *byte, size int64) (hchan chan any)
func makechan(chanType *byte, size int) (hchan chan any)

Author: stream
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source stream !
  TOC