OLD | NEW |
1 <!--{ | 1 <!--{ |
2 "Title": "The Go Programming Language Specification", | 2 "Title": "The Go Programming Language Specification", |
3 » "Subtitle": "Version of August 17, 2012", | 3 » "Subtitle": "Version of August 29, 2012", |
4 "Path": "/ref/spec" | 4 "Path": "/ref/spec" |
5 }--> | 5 }--> |
6 | 6 |
7 <!-- | 7 <!-- |
8 TODO | 8 TODO |
9 [ ] need language about function/method calls and parameter passing rules | 9 [ ] need language about function/method calls and parameter passing rules |
10 [ ] last paragraph of #Assignments (constant promotion) should be elsewhere | 10 [ ] last paragraph of #Assignments (constant promotion) should be elsewhere |
11 and mention assignment to empty interface. | 11 and mention assignment to empty interface. |
12 [ ] need to say something about "scope" of selectors? | 12 [ ] need to say something about "scope" of selectors? |
13 [ ] clarify what a field name is in struct declarations | 13 [ ] clarify what a field name is in struct declarations |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 </p> | 81 </p> |
82 | 82 |
83 <h2 id="Source_code_representation">Source code representation</h2> | 83 <h2 id="Source_code_representation">Source code representation</h2> |
84 | 84 |
85 <p> | 85 <p> |
86 Source code is Unicode text encoded in | 86 Source code is Unicode text encoded in |
87 <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. The text is not | 87 <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. The text is not |
88 canonicalized, so a single accented code point is distinct from the | 88 canonicalized, so a single accented code point is distinct from the |
89 same character constructed from combining an accent and a letter; | 89 same character constructed from combining an accent and a letter; |
90 those are treated as two code points. For simplicity, this document | 90 those are treated as two code points. For simplicity, this document |
91 will use the term <i>character</i> to refer to a Unicode code point. | 91 will use the unqualified term <i>character</i> to refer to a Unicode code point |
| 92 in the source text. |
92 </p> | 93 </p> |
93 <p> | 94 <p> |
94 Each code point is distinct; for instance, upper and lower case letters | 95 Each code point is distinct; for instance, upper and lower case letters |
95 are different characters. | 96 are different characters. |
96 </p> | 97 </p> |
97 <p> | 98 <p> |
98 Implementation restriction: For compatibility with other tools, a | 99 Implementation restriction: For compatibility with other tools, a |
99 compiler may disallow the NUL character (U+0000) in the source text. | 100 compiler may disallow the NUL character (U+0000) in the source text. |
100 </p> | 101 </p> |
101 | 102 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 </p> | 191 </p> |
191 <ul> | 192 <ul> |
192 <li>an | 193 <li>an |
193 <a href="#Identifiers">identifier</a> | 194 <a href="#Identifiers">identifier</a> |
194 </li> | 195 </li> |
195 | 196 |
196 <li>an | 197 <li>an |
197 <a href="#Integer_literals">integer</a>, | 198 <a href="#Integer_literals">integer</a>, |
198 <a href="#Floating-point_literals">floating-point</a>, | 199 <a href="#Floating-point_literals">floating-point</a>, |
199 <a href="#Imaginary_literals">imaginary</a>, | 200 <a href="#Imaginary_literals">imaginary</a>, |
200 » <a href="#Character_literals">character</a>, or | 201 » <a href="#Rune_literals">rune</a>, or |
201 <a href="#String_literals">string</a> literal | 202 <a href="#String_literals">string</a> literal |
202 </li> | 203 </li> |
203 | 204 |
204 <li>one of the <a href="#Keywords">keywords</a> | 205 <li>one of the <a href="#Keywords">keywords</a> |
205 <code>break</code>, | 206 <code>break</code>, |
206 <code>continue</code>, | 207 <code>continue</code>, |
207 <code>fallthrough</code>, or | 208 <code>fallthrough</code>, or |
208 <code>return</code> | 209 <code>return</code> |
209 </li> | 210 </li> |
210 | 211 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 0.i | 353 0.i |
353 2.71828i | 354 2.71828i |
354 1.e+0i | 355 1.e+0i |
355 6.67428e-11i | 356 6.67428e-11i |
356 1E6i | 357 1E6i |
357 .25i | 358 .25i |
358 .12345E+5i | 359 .12345E+5i |
359 </pre> | 360 </pre> |
360 | 361 |
361 | 362 |
362 <h3 id="Character_literals">Character literals</h3> | 363 <h3 id="Rune_literals">Rune literals</h3> |
363 | 364 |
364 <p> | 365 <p> |
365 A character literal represents a <a href="#Constants">character constant</a>, | 366 A rune literal represents a <a href="#Constants">rune constant</a>, |
366 typically a Unicode code point, as one or more characters enclosed in single | 367 an integer value identifying a Unicode code point. |
367 quotes. Within the quotes, any character may appear except single | 368 A rune literal is expressed as one or more characters enclosed in single quotes. |
368 quote and newline. A single quoted character represents itself, | 369 Within the quotes, any character may appear except single |
| 370 quote and newline. A single quoted character represents the Unicode value |
| 371 of the character itself, |
369 while multi-character sequences beginning with a backslash encode | 372 while multi-character sequences beginning with a backslash encode |
370 values in various formats. | 373 values in various formats. |
371 </p> | 374 </p> |
372 <p> | 375 <p> |
373 The simplest form represents the single character within the quotes; | 376 The simplest form represents the single character within the quotes; |
374 since Go source text is Unicode characters encoded in UTF-8, multiple | 377 since Go source text is Unicode characters encoded in UTF-8, multiple |
375 UTF-8-encoded bytes may represent a single integer value. For | 378 UTF-8-encoded bytes may represent a single integer value. For |
376 instance, the literal <code>'a'</code> holds a single byte representing | 379 instance, the literal <code>'a'</code> holds a single byte representing |
377 a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while | 380 a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while |
378 <code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) represent
ing | 381 <code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) represent
ing |
379 a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>. | 382 a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>. |
380 </p> | 383 </p> |
381 <p> | 384 <p> |
382 Several backslash escapes allow arbitrary values to be represented | 385 Several backslash escapes allow arbitrary values to be encoded as |
383 as ASCII text. There are four ways to represent the integer value | 386 as ASCII text. There are four ways to represent the integer value |
384 as a numeric constant: <code>\x</code> followed by exactly two hexadecimal | 387 as a numeric constant: <code>\x</code> followed by exactly two hexadecimal |
385 digits; <code>\u</code> followed by exactly four hexadecimal digits; | 388 digits; <code>\u</code> followed by exactly four hexadecimal digits; |
386 <code>\U</code> followed by exactly eight hexadecimal digits, and a | 389 <code>\U</code> followed by exactly eight hexadecimal digits, and a |
387 plain backslash <code>\</code> followed by exactly three octal digits. | 390 plain backslash <code>\</code> followed by exactly three octal digits. |
388 In each case the value of the literal is the value represented by | 391 In each case the value of the literal is the value represented by |
389 the digits in the corresponding base. | 392 the digits in the corresponding base. |
390 </p> | 393 </p> |
391 <p> | 394 <p> |
392 Although these representations all result in an integer, they have | 395 Although these representations all result in an integer, they have |
393 different valid ranges. Octal escapes must represent a value between | 396 different valid ranges. Octal escapes must represent a value between |
394 0 and 255 inclusive. Hexadecimal escapes satisfy this condition | 397 0 and 255 inclusive. Hexadecimal escapes satisfy this condition |
395 by construction. The escapes <code>\u</code> and <code>\U</code> | 398 by construction. The escapes <code>\u</code> and <code>\U</code> |
396 represent Unicode code points so within them some values are illegal, | 399 represent Unicode code points so within them some values are illegal, |
397 in particular those above <code>0x10FFFF</code> and surrogate halves. | 400 in particular those above <code>0x10FFFF</code> and surrogate halves. |
398 </p> | 401 </p> |
399 <p> | 402 <p> |
400 After a backslash, certain single-character escapes represent special values: | 403 After a backslash, certain single-character escapes represent special values: |
401 </p> | 404 </p> |
402 <pre class="grammar"> | 405 <pre class="grammar"> |
403 \a U+0007 alert or bell | 406 \a U+0007 alert or bell |
404 \b U+0008 backspace | 407 \b U+0008 backspace |
405 \f U+000C form feed | 408 \f U+000C form feed |
406 \n U+000A line feed or newline | 409 \n U+000A line feed or newline |
407 \r U+000D carriage return | 410 \r U+000D carriage return |
408 \t U+0009 horizontal tab | 411 \t U+0009 horizontal tab |
409 \v U+000b vertical tab | 412 \v U+000b vertical tab |
410 \\ U+005c backslash | 413 \\ U+005c backslash |
411 \' U+0027 single quote (valid escape only within character literals) | 414 \' U+0027 single quote (valid escape only within rune literals) |
412 \" U+0022 double quote (valid escape only within string literals) | 415 \" U+0022 double quote (valid escape only within string literals) |
413 </pre> | 416 </pre> |
414 <p> | 417 <p> |
415 All other sequences starting with a backslash are illegal inside character liter
als. | 418 All other sequences starting with a backslash are illegal inside rune literals. |
416 </p> | 419 </p> |
417 <pre class="ebnf"> | 420 <pre class="ebnf"> |
418 char_lit = "'" ( unicode_value | byte_value ) "'" . | 421 char_lit = "'" ( unicode_value | byte_value ) "'" . |
419 unicode_value = unicode_char | little_u_value | big_u_value | escaped_char . | 422 unicode_value = unicode_char | little_u_value | big_u_value | escaped_char . |
420 byte_value = octal_byte_value | hex_byte_value . | 423 byte_value = octal_byte_value | hex_byte_value . |
421 octal_byte_value = `\` octal_digit octal_digit octal_digit . | 424 octal_byte_value = `\` octal_digit octal_digit octal_digit . |
422 hex_byte_value = `\` "x" hex_digit hex_digit . | 425 hex_byte_value = `\` "x" hex_digit hex_digit . |
423 little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit . | 426 little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit . |
424 big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit | 427 big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit |
425 hex_digit hex_digit hex_digit hex_digit . | 428 hex_digit hex_digit hex_digit hex_digit . |
426 escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `
"` ) . | 429 escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `
"` ) . |
427 </pre> | 430 </pre> |
428 | 431 |
429 <pre> | 432 <pre> |
430 'a' | 433 'a' |
431 'ä' | 434 'ä' |
432 '本' | 435 '本' |
433 '\t' | 436 '\t' |
434 '\000' | 437 '\000' |
435 '\007' | 438 '\007' |
436 '\377' | 439 '\377' |
437 '\x07' | 440 '\x07' |
438 '\xff' | 441 '\xff' |
439 '\u12e4' | 442 '\u12e4' |
440 '\U00101234' | 443 '\U00101234' |
| 444 'aa' // illegal: too many characters |
| 445 '\xa' // illegal: too few hexadecimal digits |
| 446 '\0' // illegal: too few octal digits |
| 447 '\uDFFF' // illegal: surrogate half |
| 448 '\U00110000' // illegal: invalid Unicode code point |
441 </pre> | 449 </pre> |
442 | 450 |
443 | 451 |
444 <h3 id="String_literals">String literals</h3> | 452 <h3 id="String_literals">String literals</h3> |
445 | 453 |
446 <p> | 454 <p> |
447 A string literal represents a <a href="#Constants">string constant</a> | 455 A string literal represents a <a href="#Constants">string constant</a> |
448 obtained from concatenating a sequence of characters. There are two forms: | 456 obtained from concatenating a sequence of characters. There are two forms: |
449 raw string literals and interpreted string literals. | 457 raw string literals and interpreted string literals. |
450 </p> | 458 </p> |
451 <p> | 459 <p> |
452 Raw string literals are character sequences between back quotes | 460 Raw string literals are character sequences between back quotes |
453 <code>``</code>. Within the quotes, any character is legal except | 461 <code>``</code>. Within the quotes, any character is legal except |
454 back quote. The value of a raw string literal is the | 462 back quote. The value of a raw string literal is the |
455 string composed of the uninterpreted characters between the quotes; | 463 string composed of the uninterpreted (implicitly UTF-8-encoded) characters |
| 464 between the quotes; |
456 in particular, backslashes have no special meaning and the string may | 465 in particular, backslashes have no special meaning and the string may |
457 contain newlines. | 466 contain newlines. |
458 Carriage returns inside raw string literals | 467 Carriage returns inside raw string literals |
459 are discarded from the raw string value. | 468 are discarded from the raw string value. |
460 </p> | 469 </p> |
461 <p> | 470 <p> |
462 Interpreted string literals are character sequences between double | 471 Interpreted string literals are character sequences between double |
463 quotes <code>""</code>. The text between the quotes, | 472 quotes <code>""</code>. The text between the quotes, |
464 which may not contain newlines, forms the | 473 which may not contain newlines, forms the |
465 value of the literal, with backslash escapes interpreted as they | 474 value of the literal, with backslash escapes interpreted as they |
466 are in character literals (except that <code>\'</code> is illegal and | 475 are in rune literals (except that <code>\'</code> is illegal and |
467 <code>\"</code> is legal). The three-digit octal (<code>\</code><i>nnn</i>) | 476 <code>\"</code> is legal), with the same restrictions. |
| 477 The three-digit octal (<code>\</code><i>nnn</i>) |
468 and two-digit hexadecimal (<code>\x</code><i>nn</i>) escapes represent individua
l | 478 and two-digit hexadecimal (<code>\x</code><i>nn</i>) escapes represent individua
l |
469 <i>bytes</i> of the resulting string; all other escapes represent | 479 <i>bytes</i> of the resulting string; all other escapes represent |
470 the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>. | 480 the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>. |
471 Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent | 481 Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent |
472 a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>, | 482 a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>, |
473 <code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent | 483 <code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent |
474 the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of chara
cter | 484 the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of chara
cter |
475 U+00FF. | 485 U+00FF. |
476 </p> | 486 </p> |
477 | 487 |
478 <pre class="ebnf"> | 488 <pre class="ebnf"> |
479 string_lit = raw_string_lit | interpreted_string_lit . | 489 string_lit = raw_string_lit | interpreted_string_lit . |
480 raw_string_lit = "`" { unicode_char | newline } "`" . | 490 raw_string_lit = "`" { unicode_char | newline } "`" . |
481 interpreted_string_lit = `"` { unicode_value | byte_value } `"` . | 491 interpreted_string_lit = `"` { unicode_value | byte_value } `"` . |
482 </pre> | 492 </pre> |
483 | 493 |
484 <pre> | 494 <pre> |
485 `abc` // same as "abc" | 495 `abc` // same as "abc" |
486 `\n | 496 `\n |
487 \n` // same as "\\n\n\\n" | 497 \n` // same as "\\n\n\\n" |
488 "\n" | 498 "\n" |
489 "" | 499 "" |
490 "Hello, world!\n" | 500 "Hello, world!\n" |
491 "日本語" | 501 "日本語" |
492 "\u65e5本\U00008a9e" | 502 "\u65e5本\U00008a9e" |
493 "\xff\u00FF" | 503 "\xff\u00FF" |
| 504 "\uD800" // illegal: surrogate half |
| 505 "\U00110000" // illegal: invalid Unicode code point |
494 </pre> | 506 </pre> |
495 | 507 |
496 <p> | 508 <p> |
497 These examples all represent the same string: | 509 These examples all represent the same string: |
498 </p> | 510 </p> |
499 | 511 |
500 <pre> | 512 <pre> |
501 "日本語" // UTF-8 input text | 513 "日本語" // UTF-8 input text |
502 `日本語` // UTF-8 input text as a raw literal | 514 `日本語` // UTF-8 input text as a raw literal |
503 "\u65e5\u672c\u8a9e" // The explicit Unicode code points | 515 "\u65e5\u672c\u8a9e" // the explicit Unicode code points |
504 "\U000065e5\U0000672c\U00008a9e" // The explicit Unicode code points | 516 "\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points |
505 "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // The explicit UTF-8 bytes | 517 "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes |
506 </pre> | 518 </pre> |
507 | 519 |
508 <p> | 520 <p> |
509 If the source code represents a character as two code points, such as | 521 If the source code represents a character as two code points, such as |
510 a combining form involving an accent and a letter, the result will be | 522 a combining form involving an accent and a letter, the result will be |
511 an error if placed in a character literal (it is not a single code | 523 an error if placed in a rune literal (it is not a single code |
512 point), and will appear as two code points if placed in a string | 524 point), and will appear as two code points if placed in a string |
513 literal. | 525 literal. |
514 </p> | 526 </p> |
515 | 527 |
516 | 528 |
517 <h2 id="Constants">Constants</h2> | 529 <h2 id="Constants">Constants</h2> |
518 | 530 |
519 <p>There are <i>boolean constants</i>, | 531 <p>There are <i>boolean constants</i>, |
520 <i>character constants</i>, | 532 <i>rune constants</i>, |
521 <i>integer constants</i>, | 533 <i>integer constants</i>, |
522 <i>floating-point constants</i>, <i>complex constants</i>, | 534 <i>floating-point constants</i>, <i>complex constants</i>, |
523 and <i>string constants</i>. Character, integer, floating-point, | 535 and <i>string constants</i>. Character, integer, floating-point, |
524 and complex constants are | 536 and complex constants are |
525 collectively called <i>numeric constants</i>. | 537 collectively called <i>numeric constants</i>. |
526 </p> | 538 </p> |
527 | 539 |
528 <p> | 540 <p> |
529 A constant value is represented by a | 541 A constant value is represented by a |
530 <a href="#Character_literals">character</a>, | 542 <a href="#Rune_literals">rune</a>, |
531 <a href="#Integer_literals">integer</a>, | 543 <a href="#Integer_literals">integer</a>, |
532 <a href="#Floating-point_literals">floating-point</a>, | 544 <a href="#Floating-point_literals">floating-point</a>, |
533 <a href="#Imaginary_literals">imaginary</a>, | 545 <a href="#Imaginary_literals">imaginary</a>, |
534 or | 546 or |
535 <a href="#String_literals">string</a> literal, | 547 <a href="#String_literals">string</a> literal, |
536 an identifier denoting a constant, | 548 an identifier denoting a constant, |
537 a <a href="#Constant_expressions">constant expression</a>, | 549 a <a href="#Constant_expressions">constant expression</a>, |
538 a <a href="#Conversions">conversion</a> with a result that is a constant, or | 550 a <a href="#Conversions">conversion</a> with a result that is a constant, or |
539 the result value of some built-in functions such as | 551 the result value of some built-in functions such as |
540 <code>unsafe.Sizeof</code> applied to any value, | 552 <code>unsafe.Sizeof</code> applied to any value, |
(...skipping 1844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2385 <a href="#Method_sets">method set</a> of <code>I</code>, the selector | 2397 <a href="#Method_sets">method set</a> of <code>I</code>, the selector |
2386 expression is illegal. | 2398 expression is illegal. |
2387 </li> | 2399 </li> |
2388 <li> | 2400 <li> |
2389 In all other cases, <code>x.f</code> is illegal. | 2401 In all other cases, <code>x.f</code> is illegal. |
2390 </li> | 2402 </li> |
2391 <li> | 2403 <li> |
2392 If <code>x</code> is of pointer or interface type and has the value | 2404 If <code>x</code> is of pointer or interface type and has the value |
2393 <code>nil</code>, assigning to, evaluating, or calling <code>x.f</code> | 2405 <code>nil</code>, assigning to, evaluating, or calling <code>x.f</code> |
2394 causes a <a href="#Run_time_panics">run-time panic</a>. | 2406 causes a <a href="#Run_time_panics">run-time panic</a>. |
2395 </i> | 2407 </li> |
2396 </ol> | 2408 </ol> |
2397 | 2409 |
2398 <p> | 2410 <p> |
2399 Selectors automatically <a href="#Address_operators">dereference</a> | 2411 Selectors automatically <a href="#Address_operators">dereference</a> |
2400 pointers to structs. | 2412 pointers to structs. |
2401 If <code>x</code> is a pointer to a struct, <code>x.y</code> | 2413 If <code>x</code> is a pointer to a struct, <code>x.y</code> |
2402 is shorthand for <code>(*x).y</code>; if the field <code>y</code> | 2414 is shorthand for <code>(*x).y</code>; if the field <code>y</code> |
2403 is also a pointer to a struct, <code>x.y.z</code> is shorthand | 2415 is also a pointer to a struct, <code>x.y.z</code> is shorthand |
2404 for <code>(*(*x).y).z</code>, and so on. | 2416 for <code>(*(*x).y).z</code>, and so on. |
2405 If <code>x</code> contains an anonymous field of type <code>*A</code>, | 2417 If <code>x</code> contains an anonymous field of type <code>*A</code>, |
(...skipping 1173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3579 Constant expressions may contain only <a href="#Constants">constant</a> | 3591 Constant expressions may contain only <a href="#Constants">constant</a> |
3580 operands and are evaluated at compile-time. | 3592 operands and are evaluated at compile-time. |
3581 </p> | 3593 </p> |
3582 | 3594 |
3583 <p> | 3595 <p> |
3584 Untyped boolean, numeric, and string constants may be used as operands | 3596 Untyped boolean, numeric, and string constants may be used as operands |
3585 wherever it is legal to use an operand of boolean, numeric, or string type, | 3597 wherever it is legal to use an operand of boolean, numeric, or string type, |
3586 respectively. | 3598 respectively. |
3587 Except for shift operations, if the operands of a binary operation are | 3599 Except for shift operations, if the operands of a binary operation are |
3588 different kinds of untyped constants, the operation and, for non-boolean operati
ons, the result use | 3600 different kinds of untyped constants, the operation and, for non-boolean operati
ons, the result use |
3589 the kind that appears later in this list: integer, character, floating-point, co
mplex. | 3601 the kind that appears later in this list: integer, rune, floating-point, complex
. |
3590 For example, an untyped integer constant divided by an | 3602 For example, an untyped integer constant divided by an |
3591 untyped complex constant yields an untyped complex constant. | 3603 untyped complex constant yields an untyped complex constant. |
3592 </p> | 3604 </p> |
3593 | 3605 |
3594 <p> | 3606 <p> |
3595 A constant <a href="#Comparison_operators">comparison</a> always yields | 3607 A constant <a href="#Comparison_operators">comparison</a> always yields |
3596 an untyped boolean constant. If the left operand of a constant | 3608 an untyped boolean constant. If the left operand of a constant |
3597 <a href="#Operators">shift expression</a> is an untyped constant, the | 3609 <a href="#Operators">shift expression</a> is an untyped constant, the |
3598 result is an integer constant; otherwise it is a constant of the same | 3610 result is an integer constant; otherwise it is a constant of the same |
3599 type as the left operand, which must be of integer type | 3611 type as the left operand, which must be of integer type |
3600 (§<a href="#Arithmetic_operators">Arithmetic operators</a>). | 3612 (§<a href="#Arithmetic_operators">Arithmetic operators</a>). |
3601 Applying all other operators to untyped constants results in an untyped | 3613 Applying all other operators to untyped constants results in an untyped |
3602 constant of the same kind (that is, a boolean, integer, floating-point, | 3614 constant of the same kind (that is, a boolean, integer, floating-point, |
3603 complex, or string constant). | 3615 complex, or string constant). |
3604 </p> | 3616 </p> |
3605 | 3617 |
3606 <pre> | 3618 <pre> |
3607 const a = 2 + 3.0 // a == 5.0 (untyped floating-point constant) | 3619 const a = 2 + 3.0 // a == 5.0 (untyped floating-point constant) |
3608 const b = 15 / 4 // b == 3 (untyped integer constant) | 3620 const b = 15 / 4 // b == 3 (untyped integer constant) |
3609 const c = 15 / 4.0 // c == 3.75 (untyped floating-point constant) | 3621 const c = 15 / 4.0 // c == 3.75 (untyped floating-point constant) |
3610 const Θ float64 = 3/2 // Θ == 1.5 (type float64) | 3622 const Θ float64 = 3/2 // Θ == 1.5 (type float64) |
3611 const d = 1 << 3.0 // d == 8 (untyped integer constant) | 3623 const d = 1 << 3.0 // d == 8 (untyped integer constant) |
3612 const e = 1.0 << 3 // e == 8 (untyped integer constant) | 3624 const e = 1.0 << 3 // e == 8 (untyped integer constant) |
3613 const f = int32(1) << 33 // f == 0 (type int32) | 3625 const f = int32(1) << 33 // f == 0 (type int32) |
3614 const g = float64(2) >> 1 // illegal (float64(2) is a typed floating-p
oint constant) | 3626 const g = float64(2) >> 1 // illegal (float64(2) is a typed floating-p
oint constant) |
3615 const h = "foo" > "bar" // h == true (untyped boolean constant) | 3627 const h = "foo" > "bar" // h == true (untyped boolean constant) |
3616 const j = true // j == true (untyped boolean constant) | 3628 const j = true // j == true (untyped boolean constant) |
3617 const k = 'w' + 1 // k == 'x' (untyped character constant) | 3629 const k = 'w' + 1 // k == 'x' (untyped rune constant) |
3618 const l = "hi" // l == "hi" (untyped string constant) | 3630 const l = "hi" // l == "hi" (untyped string constant) |
3619 const m = string(k) // m == "x" (type string) | 3631 const m = string(k) // m == "x" (type string) |
3620 const Σ = 1 - 0.707i // (untyped complex constant) | 3632 const Σ = 1 - 0.707i // (untyped complex constant) |
3621 const Δ = Σ + 2.0e-4 // (untyped complex constant) | 3633 const Δ = Σ + 2.0e-4 // (untyped complex constant) |
3622 const Φ = iota*1i - 1/1i // (untyped complex constant) | 3634 const Φ = iota*1i - 1/1i // (untyped complex constant) |
3623 </pre> | 3635 </pre> |
3624 | 3636 |
3625 <p> | 3637 <p> |
3626 Applying the built-in function <code>complex</code> to untyped | 3638 Applying the built-in function <code>complex</code> to untyped |
3627 integer, character, or floating-point constants yields | 3639 integer, rune, or floating-point constants yields |
3628 an untyped complex constant. | 3640 an untyped complex constant. |
3629 </p> | 3641 </p> |
3630 | 3642 |
3631 <pre> | 3643 <pre> |
3632 const ic = complex(0, c) // ic == 3.75i (untyped complex constant) | 3644 const ic = complex(0, c) // ic == 3.75i (untyped complex constant) |
3633 const iΘ = complex(0, Θ) // iΘ == 1.5i (type complex128) | 3645 const iΘ = complex(0, Θ) // iΘ == 1.5i (type complex128) |
3634 </pre> | 3646 </pre> |
3635 | 3647 |
3636 <p> | 3648 <p> |
3637 Constant expressions are always evaluated exactly; intermediate values and the | 3649 Constant expressions are always evaluated exactly; intermediate values and the |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3953 </pre> | 3965 </pre> |
3954 | 3966 |
3955 <p> | 3967 <p> |
3956 In assignments, each value must be | 3968 In assignments, each value must be |
3957 <a href="#Assignability">assignable</a> to the type of the | 3969 <a href="#Assignability">assignable</a> to the type of the |
3958 operand to which it is assigned. If an untyped <a href="#Constants">constant</a> | 3970 operand to which it is assigned. If an untyped <a href="#Constants">constant</a> |
3959 is assigned to a variable of interface type, the constant is <a href="#Conversio
ns">converted</a> | 3971 is assigned to a variable of interface type, the constant is <a href="#Conversio
ns">converted</a> |
3960 to type <code>bool</code>, <code>rune</code>, <code>int</code>, <code>float64</c
ode>, | 3972 to type <code>bool</code>, <code>rune</code>, <code>int</code>, <code>float64</c
ode>, |
3961 <code>complex128</code> or <code>string</code> | 3973 <code>complex128</code> or <code>string</code> |
3962 respectively, depending on whether the value is a | 3974 respectively, depending on whether the value is a |
3963 boolean, character, integer, floating-point, complex, or string constant. | 3975 boolean, rune, integer, floating-point, complex, or string constant. |
3964 </p> | 3976 </p> |
3965 | 3977 |
3966 | 3978 |
3967 <h3 id="If_statements">If statements</h3> | 3979 <h3 id="If_statements">If statements</h3> |
3968 | 3980 |
3969 <p> | 3981 <p> |
3970 "If" statements specify the conditional execution of two branches | 3982 "If" statements specify the conditional execution of two branches |
3971 according to the value of a boolean expression. If the expression | 3983 according to the value of a boolean expression. If the expression |
3972 evaluates to true, the "if" branch is executed, otherwise, if | 3984 evaluates to true, the "if" branch is executed, otherwise, if |
3973 present, the "else" branch is executed. | 3985 present, the "else" branch is executed. |
(...skipping 1518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5492 </p> | 5504 </p> |
5493 | 5505 |
5494 <pre> | 5506 <pre> |
5495 uintptr(unsafe.Pointer(&x)) % unsafe.Alignof(x) == 0 | 5507 uintptr(unsafe.Pointer(&x)) % unsafe.Alignof(x) == 0 |
5496 </pre> | 5508 </pre> |
5497 | 5509 |
5498 <p> | 5510 <p> |
5499 Calls to <code>Alignof</code>, <code>Offsetof</code>, and | 5511 Calls to <code>Alignof</code>, <code>Offsetof</code>, and |
5500 <code>Sizeof</code> are compile-time constant expressions of type <code>uintptr<
/code>. | 5512 <code>Sizeof</code> are compile-time constant expressions of type <code>uintptr<
/code>. |
5501 </p> | 5513 </p> |
5502 <p> | |
5503 | 5514 |
5504 <h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3> | 5515 <h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3> |
5505 | 5516 |
5506 <p> | 5517 <p> |
5507 For the numeric types (§<a href="#Numeric_types">Numeric types</a>), the followi
ng sizes are guaranteed: | 5518 For the numeric types (§<a href="#Numeric_types">Numeric types</a>), the followi
ng sizes are guaranteed: |
5508 </p> | 5519 </p> |
5509 | 5520 |
5510 <pre class="grammar"> | 5521 <pre class="grammar"> |
5511 type size in bytes | 5522 type size in bytes |
5512 | 5523 |
(...skipping 16 matching lines...) Expand all Loading... |
5529 </li> | 5540 </li> |
5530 | 5541 |
5531 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code>
is the same as | 5542 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code>
is the same as |
5532 <code>unsafe.Alignof(x[0])</code>, but at least 1. | 5543 <code>unsafe.Alignof(x[0])</code>, but at least 1. |
5533 </li> | 5544 </li> |
5534 </ol> | 5545 </ol> |
5535 | 5546 |
5536 <p> | 5547 <p> |
5537 A struct or array type has size zero if it contains no fields (or elements, resp
ectively) that have a size greater than zero. Two distinct zero-size variables m
ay have the same address in memory. | 5548 A struct or array type has size zero if it contains no fields (or elements, resp
ectively) that have a size greater than zero. Two distinct zero-size variables m
ay have the same address in memory. |
5538 </p> | 5549 </p> |
OLD | NEW |