aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Donizetti <alb.donizetti@gmail.com>2020-07-27 14:42:06 +0200
committerAlberto Donizetti <alb.donizetti@gmail.com>2020-09-06 20:23:50 +0000
commit5cc030aa1996762e48ce446001078ce6447f105e (patch)
tree3c1c90c008a864c2e073174e8d7b22fb950f4b0e
parent617f2c3e35cdc8483b950aa3ef18d92965d63197 (diff)
downloadgo-5cc030aa1996762e48ce446001078ce6447f105e.tar.gz
go-5cc030aa1996762e48ce446001078ce6447f105e.zip
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í <mvdan@mvdan.cc>
-rw-r--r--src/go/ast/ast.go6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/go/ast/ast.go b/src/go/ast/ast.go
index 81c64589d0..1061f1d3ce 100644
--- a/src/go/ast/ast.go
+++ b/src/go/ast/ast.go
@@ -285,6 +285,12 @@ type (
}
// A BasicLit node represents a literal of basic type.
+ //
+ // Note that for the CHAR and STRING kinds, the literal is stored
+ // with its quotes. For example, for a double-quoted STRING, the
+ // first and the last rune in the Value field will be ". The
+ // Unquote and UnquoteChar functions in the strconv package can be
+ // used to unquote STRING and CHAR values, respectively.
BasicLit struct {
ValuePos token.Pos // literal position
Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING