1
1
<!--{
2
2
"Title": "The Go Programming Language Specification",
3
- "Subtitle": "Version of February 16 , 2019",
3
+ "Subtitle": "Version of March 12 , 2019",
4
4
"Path": "/ref/spec"
5
5
}-->
6
6
@@ -118,6 +118,7 @@ <h3 id="Letters_and_digits">Letters and digits</h3>
118
118
< pre class ="ebnf ">
119
119
letter = unicode_letter | "_" .
120
120
decimal_digit = "0" … "9" .
121
+ binary_digit = "0" | "1" .
121
122
octal_digit = "0" … "7" .
122
123
hex_digit = "0" … "9" | "A" … "F" | "a" … "f" .
123
124
</ pre >
@@ -273,78 +274,164 @@ <h3 id="Integer_literals">Integer literals</h3>
273
274
< p >
274
275
An integer literal is a sequence of digits representing an
275
276
< a href ="#Constants "> integer constant</ a > .
276
- An optional prefix sets a non-decimal base: < code > 0</ code > for octal, < code > 0x</ code > or
277
- < code > 0X</ code > for hexadecimal. In hexadecimal literals, letters
278
- < code > a-f</ code > and < code > A-F</ code > represent values 10 through 15.
277
+ An optional prefix sets a non-decimal base: < code > 0b</ code > or < code > 0B</ code >
278
+ for binary, < code > 0</ code > , < code > 0o</ code > , or < code > 0O</ code > for octal,
279
+ and < code > 0x</ code > or < code > 0X</ code > for hexadecimal.
280
+ A single < code > 0</ code > is considered a decimal zero.
281
+ In hexadecimal literals, letters < code > a</ code > through < code > f</ code >
282
+ and < code > A</ code > through < code > F</ code > represent values 10 through 15.
283
+ </ p >
284
+
285
+ < p >
286
+ For readability, an underscore character < code > _</ code > may appear after
287
+ a base prefix or between successive digits; such underscores do not change
288
+ the literal's value.
279
289
</ p >
280
290
< pre class ="ebnf ">
281
- int_lit = decimal_lit | octal_lit | hex_lit .
282
- decimal_lit = ( "1" … "9" ) { decimal_digit } .
283
- octal_lit = "0" { octal_digit } .
284
- hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
291
+ int_lit = decimal_lit | binary_lit | octal_lit | hex_lit .
292
+ decimal_lit = "0" | ( "1" … "9" ) [ [ "_" ] decimal_digits ] .
293
+ binary_lit = "0" ( "b" | "B" ) [ "_" ] binary_digits .
294
+ octal_lit = "0" [ "o" | "O" ] [ "_" ] octal_digits .
295
+ hex_lit = "0" ( "x" | "X" ) [ "_" ] hex_digits .
296
+
297
+ decimal_digits = decimal_digit { [ "_" ] decimal_digit } .
298
+ binary_digits = binary_digit { [ "_" ] binary_digit } .
299
+ octal_digits = octal_digit { [ "_" ] octal_digit } .
300
+ hex_digits = hex_digit { [ "_" ] hex_digit } .
285
301
</ pre >
286
302
287
303
< pre >
288
304
42
305
+ 4_2
289
306
0600
307
+ 0_600
308
+ 0o600
309
+ 0O600 // second character is capital letter 'O'
290
310
0xBadFace
311
+ 0xBad_Face
312
+ 0x_67_7a_2f_cc_40_c6
291
313
170141183460469231731687303715884105727
314
+ 170_141183_460469_231731_687303_715884_105727
315
+
316
+ _42 // an identifier, not an integer literal
317
+ 42_ // invalid: _ must separate successive digits
318
+ 4__2 // invalid: only one _ at a time
319
+ 0_xBadFace // invalid: _ must separate successive digits
292
320
</ pre >
293
321
322
+
294
323
< h3 id ="Floating-point_literals "> Floating-point literals</ h3 >
324
+
295
325
< p >
296
- A floating-point literal is a decimal representation of a
326
+ A floating-point literal is a decimal or hexadecimal representation of a
297
327
< a href ="#Constants "> floating-point constant</ a > .
298
- It has an integer part, a decimal point, a fractional part,
299
- and an exponent part. The integer and fractional part comprise
300
- decimal digits; the exponent part is an < code > e</ code > or < code > E</ code >
301
- followed by an optionally signed decimal exponent. One of the
302
- integer part or the fractional part may be elided; one of the decimal
303
- point or the exponent may be elided.
304
328
</ p >
329
+
330
+ < p >
331
+ A decimal floating-point literal consists of an integer part (decimal digits),
332
+ a decimal point, a fractional part (decimal digits), and an exponent part
333
+ (< code > e</ code > or < code > E</ code > followed by an optional sign and decimal digits).
334
+ One of the integer part or the fractional part may be elided; one of the decimal point
335
+ or the exponent part may be elided.
336
+ An exponent value exp scales the mantissa (integer and fractional part) by 10< sup > exp</ sup > .
337
+ </ p >
338
+
339
+ < p >
340
+ A hexadecimal floating-point literal consists of a < code > 0x</ code > or < code > 0X</ code >
341
+ prefix, an integer part (hexadecimal digits), a radix point, a fractional part (hexadecimal digits),
342
+ and an exponent part (< code > p</ code > or < code > P</ code > followed by an optional sign and decimal digits).
343
+ One of the integer part or the fractional part may be elided; the radix point may be elided as well,
344
+ but the exponent part is required. (This syntax matches the one given in IEEE 754-2008 §5.12.3.)
345
+ An exponent value exp scales the mantissa (integer and fractional part) by 2< sup > exp</ sup > .
346
+ </ p >
347
+
348
+ < p >
349
+ For readability, an underscore character < code > _</ code > may appear after
350
+ a base prefix or between successive digits; such underscores do not change
351
+ the literal value.
352
+ </ p >
353
+
305
354
< pre class ="ebnf ">
306
- float_lit = decimals "." [ decimals ] [ exponent ] |
307
- decimals exponent |
308
- "." decimals [ exponent ] .
309
- decimals = decimal_digit { decimal_digit } .
310
- exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
355
+ float_lit = decimal_float_lit | hex_float_lit .
356
+
357
+ decimal_float_lit = decimal_digits "." [ decimal_digits ] [ decimal_exponent ] |
358
+ decimal_digits decimal_exponent |
359
+ "." decimal_digits [ decimal_exponent ] .
360
+ decimal_exponent = ( "e" | "E" ) [ "+" | "-" ] decimal_digits .
361
+
362
+ hex_float_lit = "0" ( "x" | "X" ) hex_mantissa hex_exponent .
363
+ hex_mantissa = [ "_" ] hex_digits "." [ hex_digits ] |
364
+ [ "_" ] hex_digits |
365
+ "." hex_digits .
366
+ hex_exponent = ( "p" | "P" ) [ "+" | "-" ] decimal_digits .
311
367
</ pre >
312
368
313
369
< pre >
314
370
0.
315
371
72.40
316
- 072.40 // == 72.40
372
+ 072.40 // == 72.40
317
373
2.71828
318
374
1.e+0
319
375
6.67428e-11
320
376
1E6
321
377
.25
322
378
.12345E+5
379
+ 1_5. // == 15.0
380
+ 0.15e+0_2 // == 15.0
381
+
382
+ 0x1p-2 // == 0.25
383
+ 0x2.p10 // == 2048.0
384
+ 0x1.Fp+0 // == 1.9375
385
+ 0X.8p-0 // == 0.5
386
+ 0X_1FFFP-16 // == 0.1249847412109375
387
+ 0x15e-2 // == 0x15e - 2 (integer subtraction)
388
+
389
+ 0x.p1 // invalid: mantissa has no digits
390
+ 1p-2 // invalid: p exponent requires hexadecimal mantissa
391
+ 0x1.5e-2 // invalid: hexadecimal mantissa requires p exponent
392
+ 1_.5 // invalid: _ must separate successive digits
393
+ 1._5 // invalid: _ must separate successive digits
394
+ 1.5_e1 // invalid: _ must separate successive digits
395
+ 1.5e_1 // invalid: _ must separate successive digits
396
+ 1.5e1_ // invalid: _ must separate successive digits
323
397
</ pre >
324
398
399
+
325
400
< h3 id ="Imaginary_literals "> Imaginary literals</ h3 >
401
+
326
402
< p >
327
- An imaginary literal is a decimal representation of the imaginary part of a
403
+ An imaginary literal represents the imaginary part of a
328
404
< a href ="#Constants "> complex constant</ a > .
329
- It consists of a
330
- < a href ="#Floating-point_literals "> floating-point literal</ a >
331
- or decimal integer followed
332
- by the lower-case letter < code > i</ code > .
405
+ It consists of an < a href ="#Integer_literals "> integer</ a > or
406
+ < a href ="#Floating-point_literals "> floating-point</ a > literal
407
+ followed by the lower-case letter < code > i</ code > .
408
+ The value of an imaginary literal is the value of the respective
409
+ integer or floating-point literal multiplied by the imaginary unit < i > i</ i > .
333
410
</ p >
411
+
334
412
< pre class ="ebnf ">
335
- imaginary_lit = (decimals | float_lit) "i" .
413
+ imaginary_lit = (decimal_digits | int_lit | float_lit) "i" .
336
414
</ pre >
337
415
416
+ < p >
417
+ For backward compatibility, an imaginary literal's integer part consisting
418
+ entirely of decimal digits (and possibly underscores) is considered a decimal
419
+ integer, even if it starts with a leading < code > 0</ code > .
420
+ </ p >
421
+
338
422
< pre >
339
423
0i
340
- 011i // == 11i
424
+ 0123i // == 123i for backward-compatibility
425
+ 0o123i // == 0o123 * 1i == 83i
426
+ 0xabci // == 0xabc * 1i == 2748i
341
427
0.i
342
428
2.71828i
343
429
1.e+0i
344
430
6.67428e-11i
345
431
1E6i
346
432
.25i
347
433
.12345E+5i
434
+ 0x1p-2i // == 0x1p-2 * 1i == 0.25i
348
435
</ pre >
349
436
350
437
@@ -361,6 +448,7 @@ <h3 id="Rune_literals">Rune literals</h3>
361
448
while multi-character sequences beginning with a backslash encode
362
449
values in various formats.
363
450
</ p >
451
+
364
452
< p >
365
453
The simplest form represents the single character within the quotes;
366
454
since Go source text is Unicode characters encoded in UTF-8, multiple
@@ -370,6 +458,7 @@ <h3 id="Rune_literals">Rune literals</h3>
370
458
< code > 'ä'</ code > holds two bytes (< code > 0xc3</ code > < code > 0xa4</ code > ) representing
371
459
a literal < code > a</ code > -dieresis, U+00E4, value < code > 0xe4</ code > .
372
460
</ p >
461
+
373
462
< p >
374
463
Several backslash escapes allow arbitrary values to be encoded as
375
464
ASCII text. There are four ways to represent the integer value
@@ -380,6 +469,7 @@ <h3 id="Rune_literals">Rune literals</h3>
380
469
In each case the value of the literal is the value represented by
381
470
the digits in the corresponding base.
382
471
</ p >
472
+
383
473
< p >
384
474
Although these representations all result in an integer, they have
385
475
different valid ranges. Octal escapes must represent a value between
@@ -388,9 +478,11 @@ <h3 id="Rune_literals">Rune literals</h3>
388
478
represent Unicode code points so within them some values are illegal,
389
479
in particular those above < code > 0x10FFFF</ code > and surrogate halves.
390
480
</ p >
481
+
391
482
< p >
392
483
After a backslash, certain single-character escapes represent special values:
393
484
</ p >
485
+
394
486
< pre class ="grammar ">
395
487
\a U+0007 alert or bell
396
488
\b U+0008 backspace
@@ -403,6 +495,7 @@ <h3 id="Rune_literals">Rune literals</h3>
403
495
\' U+0027 single quote (valid escape only within rune literals)
404
496
\" U+0022 double quote (valid escape only within string literals)
405
497
</ pre >
498
+
406
499
< p >
407
500
All other sequences starting with a backslash are illegal inside rune literals.
408
501
</ p >
@@ -446,6 +539,7 @@ <h3 id="String_literals">String literals</h3>
446
539
obtained from concatenating a sequence of characters. There are two forms:
447
540
raw string literals and interpreted string literals.
448
541
</ p >
542
+
449
543
< p >
450
544
Raw string literals are character sequences between back quotes, as in
451
545
< code > `foo`</ code > . Within the quotes, any character may appear except
@@ -457,6 +551,7 @@ <h3 id="String_literals">String literals</h3>
457
551
Carriage return characters ('\r') inside raw string literals
458
552
are discarded from the raw string value.
459
553
</ p >
554
+
460
555
< p >
461
556
Interpreted string literals are character sequences between double
462
557
quotes, as in < code > "bar"</ code > .
@@ -596,6 +691,7 @@ <h2 id="Constants">Constants</h2>
596
691
internal representation with limited precision. That said, every
597
692
implementation must:
598
693
</ p >
694
+
599
695
< ul >
600
696
< li > Represent integer constants with at least 256 bits.</ li >
601
697
@@ -613,12 +709,14 @@ <h2 id="Constants">Constants</h2>
613
709
represent a floating-point or complex constant due to limits
614
710
on precision.</ li >
615
711
</ ul >
712
+
616
713
< p >
617
714
These requirements apply both to literal constants and to the result
618
715
of evaluating < a href ="#Constant_expressions "> constant
619
716
expressions</ a > .
620
717
</ p >
621
718
719
+
622
720
< h2 id ="Variables "> Variables</ h2 >
623
721
624
722
< p >
0 commit comments