Built-in function
Go new()
Returns a pointer to a new variable. With a type argument, the variable begins at its zero value. Go 1.26 also permits an expression argument, initializing the variable with that expression’s value; untyped constants receive their default type, and bare nil is not allowed. Neither form guarantees a heap allocation. The local-compatible example prints 0 then 12.
Formal Syntax & Structure
go syntaxgo
new(T) | new(expression) // expression form: Go 1.26+Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| T or expression | type or value expression | Variable type or, from Go 1.26, initial value. |
Return Value
*T
Pointer to the initialized variable.
Working Example
Example Snippetgo
package main
import "fmt"
func main() { p := new(int) fmt.Println(*p) *p = 12 fmt.Println(*p)}