Skip to content

Commit eb78f13

Browse files
adonovangopherbot
authored andcommitted
doc/go_spec.html: document new(expr)
Also, add a release note. For golang#45624 Change-Id: I1a0e111e00885c9640c073000afb72731d0930fc Reviewed-on: https://go-review.googlesource.com/c/go/+/704737 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 74cc463 commit eb78f13

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

doc/go_spec.html

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7806,12 +7806,32 @@ <h3 id="Min_and_max">Min and max</h3>
78067806
<h3 id="Allocation">Allocation</h3>
78077807

78087808
<p>
7809-
The built-in function <code>new</code> takes a type <code>T</code>,
7810-
allocates storage for a <a href="#Variables">variable</a> of that type
7811-
at run time, and returns a value of type <code>*T</code>
7812-
<a href="#Pointer_types">pointing</a> to it.
7813-
The variable is initialized as described in the section on
7814-
<a href="#The_zero_value">initial values</a>.
7809+
The built-in function <code>new</code> creates a new, initialized
7810+
<a href="#Variables">variable</a> and returns
7811+
a <a href="#Pointer_types">pointer</a> to it.
7812+
7813+
It accepts a single argument, which may be either an expression or a type.
7814+
</p>
7815+
<p>
7816+
If the argument <code>expr</code> is an expression of
7817+
type <code>T</code>, or an untyped constant expression
7818+
whose <a href="#Constants">default type</a> is <code>T</code>,
7819+
then <code>new(expr)</code> allocates a variable of
7820+
type <code>T</code>, initializes it to the value
7821+
of <code>expr</code>, and returns its address, a value of
7822+
type <code>*T</code>.
7823+
</p>
7824+
<p>
7825+
If the argument is a type <code>T</code>, then <code>new(T)</code>
7826+
allocates a variable initialized to
7827+
the <a href="#The_zero_value">zero value</a> of type <code>T</code>.
7828+
</p>
7829+
<p>
7830+
For example, <code>new(123)</code> and <code>new(int)</code> each
7831+
return a pointer to a new variable of type <code>int</code>.
7832+
7833+
The value of the first variable is <code>123</code>, and the value
7834+
of the second is <code>0</code>.
78157835
</p>
78167836

78177837
<pre class="grammar">

doc/next/2-language.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
11
## Changes to the language {#language}
22

3+
<!-- https://go.dev/issue/45624 --->
34

5+
The built-in `new` function, which creates a new variable, now allows
6+
its operand to be an expression, specifying the initial value of the
7+
variable.
8+
9+
This feature is particularly useful when working with serialization
10+
packages such as `encoding/json` or protocol buffers that use a
11+
pointer to represent an optional value, as it enables an optional
12+
field to be populated in a simple expression, for example:
13+
14+
```go
15+
import "encoding/json"
16+
17+
type Person struct {
18+
Name string `json:"name"`
19+
Age *int `json:"age"` // age if known; nil otherwise
20+
}
21+
22+
func personJSON(name string, age int) ([]byte, error) {
23+
return json.Marshal(Person{
24+
Name: name,
25+
Age: new(age),
26+
})
27+
}
28+
```

0 commit comments

Comments
 (0)