]> Cypherpunks repositories - gostls13.git/commitdiff
doc/next: improve new(expr) release note
authorAlan Donovan <adonovan@google.com>
Wed, 22 Oct 2025 21:56:32 +0000 (17:56 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 23 Oct 2025 03:30:59 +0000 (20:30 -0700)
One reader pointed out that the example isn't compelling because
&age would have worked just as well. This CL changes the example
to use a nontrivial expression. Don't nitpick the arithmetic.

For #45624

Change-Id: Icc745f5ee7000c1d3559da1388c6a5596c4d1f46
Reviewed-on: https://go-review.googlesource.com/c/go/+/714040
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
doc/next/2-language.md

index ded7becf0141c9f3142b73f603f33386facf780b..71da62f59e59ab1006d3fe6affc8159757e45e17 100644 (file)
@@ -19,10 +19,14 @@ type Person struct {
        Age  *int     `json:"age"` // age if known; nil otherwise
 }
 
-func personJSON(name string, age int) ([]byte, error) {
+func personJSON(name string, born time.Time) ([]byte, error) {
        return json.Marshal(Person{
                Name: name,
-               Age:  new(age),
+               Age:  new(yearsSince(born)),
        })
 }
+
+func yearsSince(t time.Time) int {
+       return int(time.Since(t).Hours() / (365.25 * 24)) // approximately
+}
 ```