Radar · Go · Archive · Week 38 · Sep 15 – 21, 2025
Expression to create pointer to simple types
Worth knowinglanguage
- What changed
- The proposal is to add support for
new(expr)by amending the Allocation section to say thatnew(expr)allocates a variable of typeT, initializes it to the value ofexpr, and returns its address. - Production impact
- The source does not say.
- Try it
- Write
p := new(123)and observe that*pis123. - Source
- github.com/golang/go/issues/45624
Understand it, then run it
Go lets you create a new variable and get a pointer to it with the built‑in new. Before the change you could only write new(T) where T is a type, and the new variable would be zero‑valued. The change adds a second form: new(expr). If expr is an expression of type T, new(expr) allocates a variable of type T, stores the value of expr in it, and returns a pointer to that variable. So new(123) now gives you a pointer to an int whose value is 123, just like new(int) gives you a pointer to an int whose value is 0.
Run it now
// This program demonstrates the new `new(expr)` feature.
// It prints the value of a pointer created with new(123).
package main
import "fmt"
func main() {
// Allocate an int initialized to 123 and get its address.
p := new(123)
// Dereference the pointer to read the value.
fmt.Println(*p) // Output: 123
}
What it printed when we ran it on Go 1.27.1
123
After the change ships
go · the proposal's code; it does not compile until the change ships
// This code does not compile until the change ships in Go 1.26.
package main
import "fmt"
func main() {
p := new(123)
fmt.Println(*p)
}
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed