Go 1.26's Small But Clever New Function Change!

This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).

Go 1.26 enhances the built-in `new` function to accept an initializer expression, allowing memory allocation and value assignment in a single step. Previously, developers had to allocate with `new(T)` and then dereference the pointer to assign a value, often requiring temporary variables. With Go 1.26, `new(42)` allocates an integer pointer initialized to 42 directly. This is particularly useful when working with pointer fields in structs for optional JSON/protobuf fields, partial API updates (PATCH semantics), and table-driven tests, where temporary helper variables were previously needed just to obtain a pointer to a literal value.

12m watch time
5 Comments

Sort: