Skip to content

Commit 5cc030a

Browse files
committed
go/ast: note that in BasicLit CHARs and STRINGs are quoted
The Value field of ast.BasicLit is a string field holding the literal string. For CHARs and STRINGs, the BasicLit.Value literal includes quotes, so to use the value in practise one will often need to Unquote it. Since this is a common gotcha (I've been bitten by this a few times), document it, and suggest the use of the strconv.Unquote functions. Fixes #39590 Change-Id: Ie3e13f5a2a71bb1b59e03bc5b3a16d8e2e7c01d4 Reviewed-on: https://go-review.googlesource.com/c/go/+/244960 Reviewed-by: Daniel Martí <[email protected]>
1 parent 617f2c3 commit 5cc030a

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/go/ast/ast.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ type (
285285
}
286286

287287
// A BasicLit node represents a literal of basic type.
288+
//
289+
// Note that for the CHAR and STRING kinds, the literal is stored
290+
// with its quotes. For example, for a double-quoted STRING, the
291+
// first and the last rune in the Value field will be ". The
292+
// Unquote and UnquoteChar functions in the strconv package can be
293+
// used to unquote STRING and CHAR values, respectively.
288294
BasicLit struct {
289295
ValuePos token.Pos // literal position
290296
Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING

0 commit comments

Comments
 (0)