From 8ae5d408ed62d234cb72adebb9a23e08da1cedc6 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 20 Nov 2025 14:05:27 -0800 Subject: [PATCH] spec: more precise prose for built-in function new 1) explain new(type) (simpler) before new(expr) (more complicated) 2) for new(expr), explain what happens when expr is an untyped bool 3) explain that new(nil) is not permitted 4) streamline examples slightly Fixes #76122. Change-Id: I5ddb26bd88241b4b2b9aa9b532a62f7861c2341c Reviewed-on: https://go-review.googlesource.com/c/go/+/722482 Auto-Submit: Robert Griesemer LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Griesemer Reviewed-by: Alan Donovan Commit-Queue: Robert Griesemer Reviewed-by: Ian Lance Taylor --- doc/go_spec.html | 53 ++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index b75845372d..b129520180 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -7794,40 +7794,32 @@ min(x, y, z) == min(min(x, y), z)

Allocation

- The built-in function new creates a new, initialized - variable and returns - a pointer to it. - - It accepts a single argument, which may be either an expression or a type. -

-

- If the argument expr is an expression of - type T, or an untyped constant expression - whose default type is T, - then new(expr) allocates a variable of - type T, initializes it to the value - of expr, and returns its address, a value of - type *T. +The built-in function new creates a new, initialized +variable and returns +a pointer to it. +It accepts a single argument, which may be either a type or an expression.

+

- If the argument is a type T, then new(T) - allocates a variable initialized to - the zero value of type T. +If the argument is a type T, then new(T) +allocates a variable of type T initialized to its +zero value.

-

- For example, new(123) and new(int) each - return a pointer to a new variable of type int. - The value of the first variable is 123, and the value - of the second is 0. +

+If the argument is an expression x, then new(x) +allocates a variable of the type of x initialized to the value of x. +If that value is an untyped constant, it is first implicitly converted +to its default type; +if it is an untyped boolean value, it is first implicitly converted to type bool. +The predeclared identifier nil cannot be used as an argument to new.

-
-new(T)
-
-

-For instance +For example, new(int) and new(123) each +return a pointer to a new variable of type int. +The value of the first variable is 0, and the value +of the second is 123. Similarly

@@ -7836,13 +7828,12 @@ new(S)
 

-allocates storage for a variable of type S, +allocates a variable of type S, initializes it (a=0, b=0.0), and returns a value of type *S containing the address -of the location. +of the variable.

-

Handling panics

Two built-in functions, panic and recover, -- 2.52.0