Go 1.26: The `new(expr)` Feature — Simpler Pointer Creation, Clearer Code

Go 1.26 introduces a small but elegant change: `new` can now take an expression, not just a type. Let’s explore what it means, why it…

分享
Go 1.26: The `new(expr)` Feature — Simpler Pointer Creation, Clearer Code
Generate By DALLE-3

Go 1.26 introduces a small but elegant change: `new` can now take an expression, not just a type. Let’s explore what it means, why it matters, and how the community reacted.

🚀 Introduction

In Go 1.26, the built-in function new receives a subtle yet elegant upgrade — it can now accept an expression instead of just a type.

This new syntax makes it easier to create pointers directly from values, improving readability and consistency across Go’s allocation patterns.

🧩 What Is new(expr)?

Before Go 1.26, you could only write:

p := new(int)  // returns *int, initialized to 0

Starting in Go 1.26, you can now write:

p := new(42)   // returns *int, initialized to 42

In short:

  • new(T) — allocates a zero value of type T, returns *T.
  • new(expr) — evaluates expr, copies its result into a new variable, and returns its pointer.

Try It

🧠 Examples

Simple values

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

Structs

type User struct { Name string } 
u := new(User{Name: "Alice"}) 
fmt.Println(*u) // {Alice}

Function results

f := func() int { return 99 } 
x := new(f()) 
fmt.Println(*x) // 99

Invalid

new(nil) // compile error

💡 Why Add This Feature?

  1. Less boilerplate
    Before:
v := 42 p := &v

Now:

p := new(42)
  1. Consistency
     Go already allows &T{...} for struct literals, but not &42 for basic values.
     new(expr) fills that gap without breaking language rules.
  2. Readability
     When you want “a pointer to this value,” new(expr) reads exactly as it behaves.

⚠️ Notes and Limitations

  • The expression must have a concrete type (constants will infer their type).
  • new(nil) is still illegal.
  • Avoid side effects in the expression.
  • Fully backward-compatible — no code breaks.

🌍 Community Reactions

The change sparked lively but upbeat discussion across Reddit, GitHub, and the Go proposal tracker.

👍 Overall sentiment: “Small but nice”

Most developers see it as a minor yet satisfying quality-of-life improvement, particularly those who wrote helper functions PointerTo() just to take the address of a value.

“Finally, I can delete my PointerTo() helper!” — Reddit user

✅ Supporters highlight:

  • Eliminates boilerplate pointer helpers.
  • Unifies allocation syntax with &T{}.
  • Enhances generic and testing code.
  • Improves clarity: “pointer to a value” is explicit and readable.

⚖️ Skeptics mention:

  1. Why not just &42?
     Classic debate: address-of operator on untyped constants isn’t valid since such values have no allocated memory.
     new(expr) is a safe, practical compromise.
  2. Limited scope
     Some call it “a narrow convenience.”
     Others argue that’s precisely why it’s good — low risk, low complexity, easy to adopt.
  3. Code style
     Teams agree it’s fine for occasional pointer creation, but shouldn’t replace struct literals or conventional variable declarations.

🧰 Tooling and Ecosystem

  • Linters may soon suggest replacing helper functions with new(expr).
  • Backward-compatible — no migration needed.
  • IDEs and editors already recognize the syntax.

🧭 Practical Tips

  • ✅ Use new(expr) for temporary pointers (tests, configs, small values).
  • ✅ Keep &T{} for structured initialization.
  • 🚫 Don’t overuse it.
  • 🚫 Avoid new(nil) — still invalid.

🔚 Conclusion

new(expr) is a tiny addition — yet it makes Go code feel smoother and more expressive.
It unifies how we allocate and reference values without introducing new concepts or syntax noise. Another step in Go’s ongoing evolution toward clarity through simplicity.