make用于内建类型(map、slice 和channel)的内存分配

new用于各种类型的内存分配

 

new(T)分配了零值填充的T类型的内存空间, 并且返回其地址,即一个*T类型的值

 

内建函数make(T, args)与new(T)有着不同的功能,
make只能创建slice、map和channel,
并且返回一个有初始值(非零)的T类型,而不是*T

 

本质来讲,导致这三个类型有所不同的原因是指向数据结构的引用在使用前必须被初始化

 

The way to go

new(T) allocates zeroed storage for a new item of type T and returns its address,
a value of type *T:

   it returns a pointer to a newly allocated zero value of type T, 

ready for use;

it is equivalent to &T{ }


make(T) returns an initialized value of type T;
  it applies only to the 3 built-in reference types:
slices, maps and channels (see chapters 8 and 13).

 

golang new make 区别

 

相关文章:

  • 2023-01-12
  • 2021-10-20
  • 2021-08-15
  • 2021-09-25
  • 2021-09-24
  • 2022-12-23
  • 2021-11-01
  • 2021-10-30
猜你喜欢
  • 2021-11-12
  • 2022-01-12
  • 2021-12-30
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案