Allocate memory for a new zero value of a type.
The new built-in function allocates memory. The first argument is a type, not a value, and the value returned is a pointer to a newly allocated zero value of that type.
new(T) allocates a zeroed T value and returns a *T pointing to it. This is useful when you need a pointer to a value and want it initialized to the zero value.
For composite types, make is often preferred over new. new is most useful for simple types or when you need to ensure a zero-initialized value.
| Name | Description | Optional |
|---|---|---|
Type |
The type to allocate. | No |
p := new(int)
*p = 42
fmt.Println(*p) // 42