Repository navigation
Description
This has come up before, specifically during discussions of #19308 and #28256. I am writing this down so we have a place for discussing this independently. I have no strong feelings about this proposal either way.
Proposal
This is a fully backward-compatible proposal for permitting the use of a blank (_) as a separator in number literals. Specifically, we change the integer literal syntax such that we also allow a "_" after the first digit (the change is the extra "_"):
int_lit = decimal_lit | octal_lit | hex_lit .
decimal_lit = ( "1" … "9" ) { decimal_digit | "_" } .
octal_lit = "0" { octal_digit | "_" } .
hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit | "_" } .
And we change the floating-point number literal syntax correspondingly (the change is the extra "_"):
float_lit = decimals "." [ decimals ] [ exponent ] | decimals exponent | "." decimals [ exponent ] .
decimals = decimal_digit { decimal_digit | "_" } .
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
For complex number literals the change is implied by the change to floating-point literals.
Examples:
123_456_789
1_000_000_000
0b0110_1101_1101_0010 // if we had binary integer literals
0xdead_beef
3.1415_9265
but also:
0___ // 0
0_77 // 77 decimal, or perhaps not permitted (TBD)
01_10 // 0110 (octal)
1_2__3___ // 123
0x1_23_0 // 0x1230
0__.0__e-0__ // 0.0e-0
Discussion
The notation follows more or less the syntax used in other languages (e.g., Swift).
The implementation is straight-forward (a minor change to the number scanner). As the examples show, the separator, if judiciously used, may improve readability; or it may degrade it significantly.
A tool (go vet) might impose restrictions on how the separator is used for readability.
Activity
ericlagergren commentedon Oct 30, 2018
What would be the reason for allowing the underscore to appear anywhere instead of limiting it to (multiples of) commonly used places? E.g.,
ISTM being too permissive only degrades readability.
dgryski commentedon Oct 30, 2018
@ericlagergren It seems more Go-like to leave the grammar more free-form with regards to placement of
_, but leave the enforcement of style (how many digits in each group) to tooling and code review.ericlagergren commentedon Oct 30, 2018
griesemer commentedon Oct 30, 2018
@ericlagergren What's a commonly used placement of _? That's really difficult to impossible to answer and shouldn't be decided by the language. Also, note that other languages also don't try to pin that down.
ericlagergren commentedon Oct 30, 2018
alanfo commentedon Oct 31, 2018
I think this is an excellent idea if we are to have binary literals (which otherwise become unreadable after one or two bytes) and also helps with the readability of other long numbers.
Several other languages have a similar feature.
As far as placement is concerned, we'll just have to leave that to the good taste of gophers :)
davecheney commentedon Oct 31, 2018
I don’t think this improves readability. For example, which is more readable?
x := 100000000
y := 100_000_000
const million = 1000000
z := 100 * million
The latter is, IMO, the more descriptive, leverages the qualities of untyped constants, the lineage of the time package, and exists today without new syntax.
alanfo commentedon Oct 31, 2018
Well, exact powers of 10 can always be dealt with in some other manner. One could also do this:
z2 := int(1e8)
But what about these:
// binary literals
a := 0b0101110110011011
b := 0b01011101_10011011 // bytes
c := 0b0101_1101_1001_1011 // nybbles
// decimal literals
d := 2861945612983
e := 2_861_945_612_983 // thousand separators
To my eyes, the underscored literals are much easier to read.
mundaym commentedon Oct 31, 2018
Are there any concrete examples of existing code where this would help readability significantly? Adding a separator does make sense for big numbers but are they common enough to warrant changing the language for?
I've spent a bit of time working with Swift code and in my limited experience I've seen that underscores tend to get used inconsistently. I think they end up making it harder (for me anyway) to parse the code.
Go already has quite a few ways to write constants, I'm not sure we need more.
alanfo commentedon Oct 31, 2018
When programming in other languages which have digit separators (C#, Java, Kotlin), I generally split binary literals into nybbles and add thousand separators for decimal literals >= 1,000. That more or less mimics how I'd write them down on paper (with space and comma as respective separators).
Of course, that doesn't mean I find longer numbers without separators unreadable - I'm OK up to about twice those lengths - but, when you have something, you tend to use it.
Very long decimal literals don't crop up very often (except perhaps powers of 10) but when they do it really is good to have the digit separator in the toolbox :)
If this proposal is accepted, then no doubt there will be people who abuse it or use it inconsistently but I don't really think it is practical to limit placement in some way. Other languages don't seem to bother either.
deanveloper commentedon Oct 31, 2018
international number formatting may be something to consider
RalphCorderoy commentedon Oct 31, 2018
I think it would be better to rule out adjacent or trailing underscores, and to always want at least one digit between the base indicator and the first underscore, e.g. 0_7 is invalid, effectively ruling out a leading underscore on the value's digits.
95 remaining items
spec: document new Go2 number literals
math/big: add support for underscores '_' in numbers
cmd/compile: remove work-arounds for handling underscores in numbers
gopherbot commentedon May 2, 2019
Change https://golang.org/cl/174897 mentions this issue:
cmd/compile: disable Go1.13 language features for -lang=go1.12 and belowcmd/compile: disable Go1.13 language features for -lang=go1.12 and below
rsc commentedon May 7, 2019
As a reminder, we introduced a new process for these Go 2-related language changes in our blog post blog.golang.org/go2-here-we-come. The Go 1.13 development cycle is now over and it’s time for the final decision.
The feedback on the Go 2 number literal changes has been strongly positive, with very few negative voices. These changes modernize and harmonize Go’s number literal syntax without adding significant complexity to the language: There is now a uniform prefix notation for the three common non-decimal number bases, which matches the notation used in other modern programming languages. The introduction of hexadecimal floating-point literals addresses a pain point for people concerning themselves with numeric code. The suffix “i” may now be used with any (non-imaginary) number literal to create an imaginary constant in a uniform way. And finally, underscores may be used to split longer literals into groups of digits for improved readability.
Proposal accepted for Go 1.13. Closing because the changes have landed.
- rsc for proposal review
gopherbot commentedon Jun 27, 2019
Change https://golang.org/cl/184080 mentions this issue:
text/scanner: remove AllowDigitSeparator flag againtext/scanner: remove AllowDigitSeparator flag again
eacp commentedon Jun 29, 2019
Well thats a fair point. I have done that in some JS programs
gopherbot commentedon Aug 10, 2019
Change https://golang.org/cl/189718 mentions this issue:
compiler: support new numeric literal syntaxcompiler: support new numeric literal syntax
compiler: support new numeric literal syntax