new(T) 和 &T{} 有什么区别?
What is the difference between new(T) and &T{}?
The two forms new(T) and &T{} are completely equivalent: Both allocate a zero T and return a pointer to this allocated memory. The only difference is, that &T{} doesn't work for builtin types like int; you can only do new(int).
这两种形式的new(T)和&T{}是完全等效的:两个分配零T和一个指针返回到该分配的内存。唯一的区别是,&T {}不适用于内置类型 int ; 你只能这样做new(int)。
Yes, good point. &T{} is only for composite literals (i.e., structs, arrays, slices, and maps) 是的,指针。&T {}仅适用于复合类型(即结构体,数组,切片和集合)
Last updated
Was this helpful?