From 747fe2efedfdc43f797b4fc1672e29ff7a01ec6a Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Fri, 24 Oct 2025 10:55:48 -0700 Subject: [PATCH] encoding/json/v2: fix typo in documentation about errors.AsType CL 708495 mass migrated the stdlib to use errors.AsType. It rewrote the documentation, but uses the non-pointer type of SemanticError or SyntacticError, which is invalid since the Error method is declared on the pointer receiver. Also, call errors.AsType on a separate line for readability. Change-Id: I2eaf59614e2b58aa4bc8669ab81ce64168c54f13 Reviewed-on: https://go-review.googlesource.com/c/go/+/714660 Reviewed-by: David Chase Reviewed-by: Dmitri Shuralyov Reviewed-by: Johan Brandhorst-Satzkorn Auto-Submit: Joseph Tsai LUCI-TryBot-Result: Go LUCI --- src/encoding/json/jsontext/state.go | 3 ++- src/encoding/json/v2/errors.go | 3 ++- src/encoding/json/v2/example_test.go | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/encoding/json/jsontext/state.go b/src/encoding/json/jsontext/state.go index 538dfe32bfa..e93057a34c5 100644 --- a/src/encoding/json/jsontext/state.go +++ b/src/encoding/json/jsontext/state.go @@ -24,7 +24,8 @@ import ( // The name of a duplicate JSON object member can be extracted as: // // err := ... -// if serr, ok := errors.AsType[jsontext.SyntacticError](err); ok && serr.Err == jsontext.ErrDuplicateName { +// serr, ok := errors.AsType[*jsontext.SyntacticError](err) +// if ok && serr.Err == jsontext.ErrDuplicateName { // ptr := serr.JSONPointer // JSON pointer to duplicate name // name := ptr.LastToken() // duplicate name itself // ... diff --git a/src/encoding/json/v2/errors.go b/src/encoding/json/v2/errors.go index 4421f8b821c..4895386fe2c 100644 --- a/src/encoding/json/v2/errors.go +++ b/src/encoding/json/v2/errors.go @@ -29,7 +29,8 @@ import ( // The name of an unknown JSON object member can be extracted as: // // err := ... -// if serr, ok := errors.AsType[json.SemanticError](err); ok && serr.Err == json.ErrUnknownName { +// serr, ok := errors.AsType[*json.SemanticError](err) +// if ok && serr.Err == json.ErrUnknownName { // ptr := serr.JSONPointer // JSON pointer to unknown name // name := ptr.LastToken() // unknown name itself // ... diff --git a/src/encoding/json/v2/example_test.go b/src/encoding/json/v2/example_test.go index 6d539bbd36b..dc1f06674ce 100644 --- a/src/encoding/json/v2/example_test.go +++ b/src/encoding/json/v2/example_test.go @@ -371,7 +371,8 @@ func Example_unknownMembers() { // Specifying RejectUnknownMembers causes Unmarshal // to reject the presence of any unknown members. err = json.Unmarshal([]byte(input), new(Color), json.RejectUnknownMembers(true)) - if serr, ok := errors.AsType[*json.SemanticError](err); ok && serr.Err == json.ErrUnknownName { + serr, ok := errors.AsType[*json.SemanticError](err) + if ok && serr.Err == json.ErrUnknownName { fmt.Println("Unmarshal error:", serr.Err, strconv.Quote(serr.JSONPointer.LastToken())) }