Go 1.26 Extends built‑in new to Accept Arbitrary Expressions
Go 1.26 adds a new capability to the built‑in new function, allowing it to take any expression, copy the result into a temporary variable and return a pointer, which eliminates the need for helper functions, prevents hidden memory leaks and yields measurable performance gains, as shown by concrete benchmarks and compiler‑level escape‑analysis explanations.
In large Go codebases that frequently interact with JSON, gRPC or YAML (for example, Kubernetes), developers often write helper functions such as getPointerValue[T any](v T) *T { return &v } because the language forbids taking the address of a literal or a temporary value. The helper simply returns a pointer to a copy of its argument.
This pattern has several drawbacks. First, when a struct field is a pointer used to indicate “unset” (nil) versus the zero value, assigning a literal address like d.Num = &12345 is a compile‑time error; the helper must be used instead. Second, the pointer may keep large objects alive: if d1.Num = &bigObj.Num or d2.Num = &bigSlice[1000], the entire bigObj or bigSlice cannot be freed, effectively causing a memory leak.
Go 1.26 changes the built‑in new so that its argument can be any expression. The compiler creates a temporary variable in the current scope, copies the expression result into it, and returns the address of that temporary. Example usages are:
p1 := new(1234) // *int pointing to 1234
func getString() string { return "apocelipes" }
p2 := new(getString()) // *string pointing to "apocelipes"
s := "Hello, "
p3 := new(s + getString() + "!") // *string pointing to "Hello, apocelipes!"The new behaviour essentially folds the former getPointerValue helper into the language primitive, simplifying code.
Performance tests compare the helper‑based approach ( BenchmarkOld) with the new built‑in ( BenchmarkNew). Both benchmarks allocate a pointer to the integer 123 and abort if the pointer is nil or the value differs. The results show that the helper incurs an extra heap allocation and runs slower because the compiler’s escape analysis treats the helper conservatively, allocating memory on the heap even when it is inlined. In contrast, the built‑in new is specially handled; the compiler rewrites it to stack‑allocated temporaries:
// new(int) becomes:
var tmp int
p1 := &tmp
// new(12345) becomes:
var tmp int
tmp = 12345
p2 := &tmpBecause the temporary variable’s lifetime is clear, escape analysis can keep it on the stack, eliminating the extra allocation and improving speed.
Thus, Go 1.26’s extension of new not only removes the need for repetitive helper functions but also provides tangible performance and memory‑management benefits, and the Go team has already promoted the feature ahead of the final release.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Golang Shines
We share daily the latest Golang technical articles, practical resources, language news, tutorials, and real-world projects to help everyone learn and improve.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
