Golang 1.26 Update: Stop Writing Pointer Helpers with new(expr)
If you write Go, you know the frustration of trying to get a pointer to a simple string or integer. For years, Go developers building JSON…
Golang 1.26 Update: Stop Writing Pointer Helpers with new(expr)
If you write Go, you know the frustration of trying to get a pointer to a simple string or integer. For years, Go developers building JSON APIs or writing tests had to rely on awkward multi-line initializations or cluttered utility packages just to get a pointer to a primitive literal.

With the release of Golang 1.26, this daily annoyance is officially over.
The Go toolchain has elegantly updated the built-in new function to accept an expression. This feature, known as new(expr), is a massive win for developer ergonomics. Let’s break down why this was a problem, how the Go 1.26 compiler handles the fix, and how it instantly cleans up your codebase.
The Problem: Why Golang Needed Pointer Helpers
To understand why this update is a game-changer, we have to look at Go’s strict memory rules.
In Go, you can only use the address-of operator (&) on a defined memory location, like a variable or a struct field. You cannot take the address of a literal value (like 42 or “active”) because they don’t have a stable memory address.
Before Go 1.18, developers had to use messy temporary variables:
status := "active"
age := 30
payload := UserUpdate{
Status: &status,
Age: &age,
}
When generics were introduced in Go 1.18, the community quickly adopted generic pointer helper functions to clean this up:
// The generic helper found in almost every Go codebase
func Ptr(v T) *T {
return &v
}
While Ptr(“active”) solved the visual clutter, it was always a workaround. It forced teams to maintain unnecessary utility packages and relied heavily on the compiler to optimize away the extra function calls.
The Solution: What is new(expr) in Go 1.26?
Golang 1.26 solves this permanently by overloading the built-in new function.
When the Go 1.26 compiler sees new(42), it automatically:
- Infers the type as an int.
- Allocates the required memory.
- Assigns the value 42 to that memory block.
- Returns the pointer to you.
Because new(expr) is built directly into the language, it works perfectly with Go’s escape analysis. Unlike custom helper functions that might accidentally force variables onto the heap (causing garbage collection overhead), the compiler knows exactly how long a new(expr) pointer lives. If it doesn’t leave the function, it stays safely on the rapid execution stack.
Real-World Example: Handling JSON Optional Fields in Go
The magic of new(expr) shines when building REST APIs or gRPC microservices that require partial data updates (like an HTTP PATCH request).
When unmarshaling JSON, you need to know the difference between a client sending a field with a zero-value (like setting an age to 0) and a client omitting the field entirely. The industry standard is to use pointers for optional fields.
Here is how painful it was to build a database update payload in Go 1.25:
type UserUpdate struct {
Status *string `json:"status,omitempty"`
Age *int `json:"age,omitempty"`
Notifications *bool `json:"notifications,omitempty"`
}
func buildActivationPayload() UserUpdate {
// Required a helper function or messy variables
activeStatus := "active"
return UserUpdate{
Status: &activeStatus,
Age: nil,
Notifications: nil,
}
}
Now, look at how clean and simple this is in Go 1.26:
func buildActivationPayload() UserUpdate {
return UserUpdate{
Status: new("active"),
Age: nil,
Notifications: nil,
}
}
The syntax new(“active”) clearly communicates your intent to allocate a new string and grab its pointer inline. It keeps your struct initializations highly readable and cohesive.
Preventing Memory Leaks: Insights from “100 Go Mistakes”
If you want to master idiomatic Go, the book 100 Go Mistakes and How to Avoid Them by Teiva Harsanyi is required reading.
In the book, Harsanyi explains how improper data structuring can cause severe memory leaks. For example, storing massive structs directly inside long-living Go maps traps that memory permanently. To fix this, developers should store pointers to the structs instead, allowing the garbage collector to free up the heavy data when it’s no longer needed.
Historically, the annoying syntax of creating pointers stopped developers from doing this. With new(expr), optimizing your memory footprint is effortless:
// Prevent map memory bloat by storing pointers
cache := make(map[string]*LargeConfig)
// Seamless inline pointer allocation
cache["service_a"] = new(LargeConfig{Threshold: 100, Enabled: true})
By removing the friction of creating pointers, Go 1.26 naturally pushes you toward better, memory-safe architecture.
Go 1.26 Performance: Faster mallocgc and Green Tea GC
Finally, Go 1.26 backs up this clean syntax with serious runtime performance upgrades.
Whenever new(expr) creates a pointer that must live on the heap, it uses the internal mallocgc allocator. In Go 1.26, mallocgc has been heavily optimized specifically for small objects (like the 8-byte integers or 16-byte strings you’ll be creating).
Furthermore, Go 1.26 enables the highly anticipated Green Tea Garbage Collector by default. This cache-optimized GC drastically reduces the CPU overhead required to scan small micro-allocations.
The result? Using new(expr) isn’t just cleaner to read — it actively runs faster than legacy workarounds.
Conclusion: Why Go 1.26 is a Game Changer
Go 1.26 proves that small, well-designed language updates can have a massive impact. By allowing the new function to accept expressions, Go removes the need for boilerplate variables and generic helper packages.
Your code becomes more declarative, more idiomatic, and thanks to the runtime optimizations, significantly faster. It’s officially time to delete your Ptr(v T) helpers and embrace the simplicity of new(expr).
If you found this blog helpful or have any questions, feel free to reach out to me on social media:
- YouTube: ElAmir’s YouTube Channel
- Facebook: ElAmir’s Facebook Page
- LinkedIn: Connect with ElAmir on LinkedIn
- Twitter: Follow ElAmir on Twitter
- Udemy: ElAmir’s Udemy Profile
메타데이터
- post_id
- 7d8296e15fb8
- slug
- golang-1-26-update-stop-writing-pointer-helpers-with-new-expr-7d8296e15fb8
- url
- https://medium.com/@elamir/golang-1-26-update-stop-writing-pointer-helpers-with-new-expr-7d8296e15fb8
- canonical_url
- https://medium.com/@elamir/golang-1-26-update-stop-writing-pointer-helpers-with-new-expr-7d8296e15fb8
- author_url
- https://medium.com/@elamir
- status
- ok
- fetched_at
- 2026-06-11 05:11:55