Skip to content

Commit ea35767

Browse files
authored
fix bug: enums of underscored number (#1581)
Signed-off-by: sdghchj <[email protected]>
1 parent e749ad5 commit ea35767

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

package.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"go/token"
66
"reflect"
77
"strconv"
8+
"strings"
89
)
910

1011
// PackageDefinitions files and definition in a package.
@@ -94,6 +95,10 @@ func (pkg *PackageDefinitions) evaluateConstValue(file *ast.File, iota int, expr
9495
case *ast.BasicLit:
9596
switch valueExpr.Kind {
9697
case token.INT:
98+
// handle underscored number, such as 1_000_000
99+
if strings.ContainsRune(valueExpr.Value, '_') {
100+
valueExpr.Value = strings.Replace(valueExpr.Value, "_", "", -1)
101+
}
97102
// hexadecimal
98103
if len(valueExpr.Value) > 2 && valueExpr.Value[0] == '0' && valueExpr.Value[1] == 'x' {
99104
if x, err := strconv.ParseInt(valueExpr.Value[2:], 16, 64); err == nil {

testdata/enums/consts/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ const octnum = 017
1010
const nonescapestr = `aa\nbb\u8888cc`
1111
const escapestr = "aa\nbb\u8888cc"
1212
const escapechar = '\u8888'
13+
const underscored = 1_000_000

0 commit comments

Comments
 (0)