Skip to content

Commit 8ae5d40

Browse files
Robert Griesemergopherbot
authored andcommitted
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Commit-Queue: Robert Griesemer <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent c5c05a0 commit 8ae5d40

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

doc/go_spec.html

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Language version go1.26 (Nov 18, 2025)",
3+
"Subtitle": "Language version go1.26 (Dec 2, 2025)",
44
"Path": "/ref/spec"
55
}-->
66

@@ -7794,40 +7794,32 @@ <h3 id="Min_and_max">Min and max</h3>
77947794
<h3 id="Allocation">Allocation</h3>
77957795

77967796
<p>
7797-
The built-in function <code>new</code> creates a new, initialized
7798-
<a href="#Variables">variable</a> and returns
7799-
a <a href="#Pointer_types">pointer</a> to it.
7800-
7801-
It accepts a single argument, which may be either an expression or a type.
7802-
</p>
7803-
<p>
7804-
If the argument <code>expr</code> is an expression of
7805-
type <code>T</code>, or an untyped constant expression
7806-
whose <a href="#Constants">default type</a> is <code>T</code>,
7807-
then <code>new(expr)</code> allocates a variable of
7808-
type <code>T</code>, initializes it to the value
7809-
of <code>expr</code>, and returns its address, a value of
7810-
type <code>*T</code>.
7797+
The built-in function <code>new</code> creates a new, initialized
7798+
<a href="#Variables">variable</a> and returns
7799+
a <a href="#Pointer_types">pointer</a> to it.
7800+
It accepts a single argument, which may be either a type or an expression.
78117801
</p>
7802+
78127803
<p>
7813-
If the argument is a type <code>T</code>, then <code>new(T)</code>
7814-
allocates a variable initialized to
7815-
the <a href="#The_zero_value">zero value</a> of type <code>T</code>.
7804+
If the argument is a type <code>T</code>, then <code>new(T)</code>
7805+
allocates a variable of type <code>T</code> initialized to its
7806+
<a href="#The_zero_value">zero value</a>.
78167807
</p>
7817-
<p>
7818-
For example, <code>new(123)</code> and <code>new(int)</code> each
7819-
return a pointer to a new variable of type <code>int</code>.
78207808

7821-
The value of the first variable is <code>123</code>, and the value
7822-
of the second is <code>0</code>.
7809+
<p>
7810+
If the argument is an expression <code>x</code>, then <code>new(x)</code>
7811+
allocates a variable of the type of <code>x</code> initialized to the value of <code>x</code>.
7812+
If that value is an untyped constant, it is first implicitly <a href="#Conversions">converted</a>
7813+
to its <a href="#Constants">default type</a>;
7814+
if it is an untyped boolean value, it is first implicitly converted to type bool.
7815+
The predeclared identifier <code>nil</code> cannot be used as an argument to <code>new</code>.
78237816
</p>
78247817

7825-
<pre class="grammar">
7826-
new(T)
7827-
</pre>
7828-
78297818
<p>
7830-
For instance
7819+
For example, <code>new(int)</code> and <code>new(123)</code> each
7820+
return a pointer to a new variable of type <code>int</code>.
7821+
The value of the first variable is <code>0</code>, and the value
7822+
of the second is <code>123</code>. Similarly
78317823
</p>
78327824

78337825
<pre>
@@ -7836,13 +7828,12 @@ <h3 id="Allocation">Allocation</h3>
78367828
</pre>
78377829

78387830
<p>
7839-
allocates storage for a variable of type <code>S</code>,
7831+
allocates a variable of type <code>S</code>,
78407832
initializes it (<code>a=0</code>, <code>b=0.0</code>),
78417833
and returns a value of type <code>*S</code> containing the address
7842-
of the location.
7834+
of the variable.
78437835
</p>
78447836

7845-
78467837
<h3 id="Handling_panics">Handling panics</h3>
78477838

78487839
<p> Two built-in functions, <code>panic</code> and <code>recover</code>,

0 commit comments

Comments
 (0)