make用于初始化slice、map、channel并返回类型本身,new用于分配任意类型的零值内存并返回指针;make做初始化工作,new仅清零。

Go 语言中 make 和 new 都用于内存分配,但用途完全不同:前者专用于初始化内置引用类型(slice、map、channel),后者用于为任意类型分配零值内存并返回指针。
make 只能用于 slice、map、channel
make 不仅分配内存,还会做初始化工作,返回的是类型本身(不是指针),且必须传入具体类型和参数:
-
slice:需指定元素类型、长度(len),可选容量(cap)s := make([]int, 5) // len=5, cap=5s := make([]int, 3, 10) // len=3, cap=10 -
map:只接受类型,可选预估容量(非必需)m := make(map[string]int)m := make(map[string]int, 16) // 建议容量,提升性能 -
channel:指定元素类型,可选缓冲区大小c := make(chan int)c := make(chan string, 10)
对其他类型(如 struct、int)用 make 会编译报错。
new 返回指向零值的指针
new(T) 为类型 T 分配内存,将所有字段置为零值,并返回 *T。它不调用构造逻辑,也不支持初始化参数:
立即学习“go语言免费学习笔记(深入)”;
p := new(int) // *int,值为 &0s := new(string) // *string,值为 &""x := new(MyStruct) // *MyStruct,所有字段为零值
注意:new 不适用于 slice/map/channel —— 它们需要额外的初始化逻辑,而 new 只做零值分配,返回的 slice/map/channel 仍是 nil,无法直接使用。
常见误用与对比示例
以下写法是错误或低效的:
bad := new([]int) // 编译通过,但返回 *[]int,底层数组未初始化,slice 仍为 nilbad := make(map[int]string, 0) // 可行,但 capacity=0 没必要,直接 make(map[int]string) 更清晰good := &struct{a int}{} // 等价于 new(struct{a int}),但更常用字面量取地址
真正需要指针且内容为零值时,new 简洁明确;要创建可用的集合类型,必须用 make。
底层本质区别
二者都向 Go 的堆(或逃逸分析后的栈)申请内存,但语义不同:
-
new(T)≈ 分配sizeof(T)字节,全填 0,返回*T -
make(T, args...)是语法糖,针对三种类型有专属逻辑:
-slice:分配底层数组 + 构建 header 结构
-map:初始化 hash 表结构,准备 bucket 数组
-channel:分配环形队列、锁、条件变量等运行时结构
所以 make 是“构造”,new 是“分配+清零”。
基本上就这些。记住:要数据结构能用,选 make;只要一个干净的指针指向零值,选 new。










