Go new vs make: Memory Allocation Differences
In the Go language, both new and make are keywords used to allocate memory, but they have different usage and target objects.
- fresh
- fresh
// 使用new分配整型变量的内存
var i *int
i = new(int)
- to create or produce
- create
// 使用make分配切片的内存
s := make([]int, 0, 10)
In summary, ‘new’ is used for memory allocation of value types, while ‘make’ is used for memory allocation of reference types.