23
聊聊Golang中的make和new
source link: https://www.tuicool.com/articles/AZbaMzF
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
概述
根据 官方文档 描述
make是用来分配并且初始化slice,map,chan等类型的对象 new也是用来分配内存的,返回对应内向的0值的指针,但并不初始化对象
看下面例子,就很明显了
a := new([]int) b := make([]int,1) fmt.Println(a) fmt.Println(b) fmt.Println(unsafe.Sizeof(*a)) fmt.Println(unsafe.Sizeof(b)+unsafe.Sizeof([1]int{})) output: &[] [0] 24 32
make
make在初始化不同类型对象时,会调用不同的函数.
make slice时会调用 runtime.makeslice
make map时会调用 runtime.makemap或者runtime.makemap_small
make chan 时会调用 runtime.makechan
b := make([]int,1) fmt.Println(b)
以slice为例,转成汇编后能看见如下代码
PCDATA $0, $0 CALL runtime.makeslice(SB) 这里 MOVQ 32(SP), AX MOVQ 24(SP), CX MOVQ 40(SP), DX
PCDATA $0, $0 CALL runtime.makemap_small(SB) MOVQ (SP), AX
new
以下面代码为例
a := new([]int) fmt.Println(a)
转成汇编语言后能看下如下代码:
PCDATA $0, $0 CALL runtime.newobject(SB) MOVQ 8(SP), AX XORPS X0, X0
可以看到最终会调用 runtime.newobject 来分配内存,并且返回指针
func newobject(typ *_type) unsafe.Pointer { return mallocgc(typ.size, typ, true) }
参考资料
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK