Fundamentals 6 min read

How Go 1.26’s new(expr) Simplifies Pointer Creation

The Go 1.26 release extends the built‑in new function to accept expressions, allowing direct allocation of pointers with initial values, which reduces boilerplate, improves readability, and introduces a more consistent syntax for developers.

Radish, Keep Going!
Radish, Keep Going!
Radish, Keep Going!
How Go 1.26’s new(expr) Simplifies Pointer Creation

Preface

Go language in the upcoming 1.26 version adds a small but interesting change to the built‑in function new: new() can now accept an expression, not only a type. This article introduces the new feature, its underlying principle, usage examples, and potential impacts and limitations.

1. Overview – What is new(expr)?

In Go 1.25 and earlier, new could only be written as: p := new(int) // returns *int, value 0 In the new version, new can be written as: p := new(42) // returns *int, value 42 In other words: new(T): allocate a zero value of type T and return *T. new(expr): evaluate expr to obtain a value v of type T, allocate a variable of type T initialized with v, and return *T.

https://go.dev/play/p/cc0aOdN_tJ4?v=gotip

2. Usage Demonstrations

Example 1: Basic Types

p1 := new(42)
fmt.Println(*p1)  // prints 42

p2 := new("hello")
fmt.Println(*p2)  // prints "hello"

Example 2: Composite Literals

p := new([]int{1, 2, 3})
fmt.Println(*p)  // prints [1 2 3]

type Person struct { Name string }
 p2 := new(Person{Name: "Alice"})
fmt.Println(*p2)  // prints {Alice}

Example 3: Function Call Expressions

f := func() string { return "Go" }
p := new(f())
fmt.Println(*p)  // prints "Go"

Note: new(nil) still results in a compilation error because nil cannot be used as an expression.

3. Design Motivation and Benefits

Reduced Boilerplate Previously, converting a simple value to a pointer required an intermediate variable:

v := 42
p := &v

Now you can write directly: p := new(42) which is more concise.

Uniformity and Consistency Allowing new(expr) makes the purpose of new(...) more flexible and uniform: it can represent “allocate by type (zero value)” as well as “allocate by expression with an initial value”.

Improved Readability In many scenarios the expression itself is clear, so writing new(expr) is more intuitive than defining a variable and then taking its address.

4. Potential Limitations and Considerations

Type Expression Capability The expr must have a determinable type at compile time (or be a constant or explicitly typed expression).

Side‑Effect Safety If the expression has side effects (function calls, assignments, etc.), its evaluation timing and semantics must be considered.

nil and Interface Scenarios Using nil , interface types, or certain complex expressions may require further clarification in the language specification.

Compatibility Concerns Although the change is backward‑compatible, some tools or analyzers might not yet support it and should be tested beforehand.

5. Conclusion

Go 1.26’s introduction of new(expr) is a minor change that brings notable syntactic simplification and consistency. It is especially convenient for everyday cases where a simple value or literal needs to be turned into a pointer.

language featureGo 1.26new(expr)pointer allocation
Radish, Keep Going!
Written by

Radish, Keep Going!

Personal sharing

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.