LEFT | RIGHT |
(no file at all) | |
1 <!--{ | 1 <!--{ |
2 "Title": "The Go Programming Language Specification", | 2 "Title": "The Go Programming Language Specification", |
3 » "Subtitle": "Version of August 5, 2014", | 3 » "Subtitle": "Version of August 28, 2014", |
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 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 in particular, backslashes have no special meaning and the string may | 472 in particular, backslashes have no special meaning and the string may |
473 contain newlines. | 473 contain newlines. |
474 Carriage return characters ('\r') inside raw string literals | 474 Carriage return characters ('\r') inside raw string literals |
475 are discarded from the raw string value. | 475 are discarded from the raw string value. |
476 </p> | 476 </p> |
477 <p> | 477 <p> |
478 Interpreted string literals are character sequences between double | 478 Interpreted string literals are character sequences between double |
479 quotes <code>""</code>. The text between the quotes, | 479 quotes <code>""</code>. The text between the quotes, |
480 which may not contain newlines, forms the | 480 which may not contain newlines, forms the |
481 value of the literal, with backslash escapes interpreted as they | 481 value of the literal, with backslash escapes interpreted as they |
482 are in rune literals (except that <code>\'</code> is illegal and | 482 are in <a href="#Rune_literals">rune literals</a> (except that <code>\'</code> i
s illegal and |
483 <code>\"</code> is legal), with the same restrictions. | 483 <code>\"</code> is legal), with the same restrictions. |
484 The three-digit octal (<code>\</code><i>nnn</i>) | 484 The three-digit octal (<code>\</code><i>nnn</i>) |
485 and two-digit hexadecimal (<code>\x</code><i>nn</i>) escapes represent individua
l | 485 and two-digit hexadecimal (<code>\x</code><i>nn</i>) escapes represent individua
l |
486 <i>bytes</i> of the resulting string; all other escapes represent | 486 <i>bytes</i> of the resulting string; all other escapes represent |
487 the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>. | 487 the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>. |
488 Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent | 488 Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent |
489 a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>, | 489 a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>, |
490 <code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent | 490 <code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent |
491 the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of chara
cter | 491 the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of chara
cter |
492 U+00FF. | 492 U+00FF. |
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1027 <h3 id="Pointer_types">Pointer types</h3> | 1027 <h3 id="Pointer_types">Pointer types</h3> |
1028 | 1028 |
1029 <p> | 1029 <p> |
1030 A pointer type denotes the set of all pointers to variables of a given | 1030 A pointer type denotes the set of all pointers to variables of a given |
1031 type, called the <i>base type</i> of the pointer. | 1031 type, called the <i>base type</i> of the pointer. |
1032 The value of an uninitialized pointer is <code>nil</code>. | 1032 The value of an uninitialized pointer is <code>nil</code>. |
1033 </p> | 1033 </p> |
1034 | 1034 |
1035 <pre class="ebnf"> | 1035 <pre class="ebnf"> |
1036 PointerType = "*" BaseType . | 1036 PointerType = "*" BaseType . |
1037 BaseType = Type . | 1037 BaseType = Type . |
1038 </pre> | 1038 </pre> |
1039 | 1039 |
1040 <pre> | 1040 <pre> |
1041 *Point | 1041 *Point |
1042 *[4]int | 1042 *[4]int |
1043 </pre> | 1043 </pre> |
1044 | 1044 |
1045 <h3 id="Function_types">Function types</h3> | 1045 <h3 id="Function_types">Function types</h3> |
1046 | 1046 |
1047 <p> | 1047 <p> |
(...skipping 1063 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2111 a <a href="#Method_expressions">method expression</a> yielding a function, | 2111 a <a href="#Method_expressions">method expression</a> yielding a function, |
2112 or a parenthesized expression. | 2112 or a parenthesized expression. |
2113 </p> | 2113 </p> |
2114 | 2114 |
2115 <p> | 2115 <p> |
2116 The <a href="#Blank_identifier">blank identifier</a> may appear as an | 2116 The <a href="#Blank_identifier">blank identifier</a> may appear as an |
2117 operand only on the left-hand side of an <a href="#Assignments">assignment</a>. | 2117 operand only on the left-hand side of an <a href="#Assignments">assignment</a>. |
2118 </p> | 2118 </p> |
2119 | 2119 |
2120 <pre class="ebnf"> | 2120 <pre class="ebnf"> |
2121 Operand = Literal | OperandName | MethodExpr | "(" Expression ")" . | 2121 Operand = Literal | OperandName | MethodExpr | "(" Expression ")" . |
2122 Literal = BasicLit | CompositeLit | FunctionLit . | 2122 Literal = BasicLit | CompositeLit | FunctionLit . |
2123 BasicLit = int_lit | float_lit | imaginary_lit | rune_lit | string_lit . | 2123 BasicLit = int_lit | float_lit | imaginary_lit | rune_lit | string_lit . |
2124 OperandName = identifier | QualifiedIdent. | 2124 OperandName = identifier | QualifiedIdent. |
2125 </pre> | 2125 </pre> |
2126 | 2126 |
2127 <h3 id="Qualified_identifiers">Qualified identifiers</h3> | 2127 <h3 id="Qualified_identifiers">Qualified identifiers</h3> |
2128 | 2128 |
2129 <p> | 2129 <p> |
2130 A qualified identifier is an identifier qualified with a package name prefix. | 2130 A qualified identifier is an identifier qualified with a package name prefix. |
2131 Both the package name and the identifier must not be | 2131 Both the package name and the identifier must not be |
2132 <a href="#Blank_identifier">blank</a>. | 2132 <a href="#Blank_identifier">blank</a>. |
2133 </p> | 2133 </p> |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2530 p.z // (*p).z | 2530 p.z // (*p).z |
2531 p.y // ((*p).T1).y | 2531 p.y // ((*p).T1).y |
2532 p.x // (*(*p).T0).x | 2532 p.x // (*(*p).T0).x |
2533 | 2533 |
2534 p.M2() // (*p).M2() | 2534 p.M2() // (*p).M2() |
2535 p.M1() // ((*p).T1).M1() | 2535 p.M1() // ((*p).T1).M1() |
2536 p.M0() // ((*p).T0).M0() | 2536 p.M0() // ((*p).T0).M0() |
2537 </pre> | 2537 </pre> |
2538 | 2538 |
2539 | 2539 |
| 2540 <h3 id="Method_expressions">Method expressions</h3> |
| 2541 |
| 2542 <p> |
| 2543 If <code>M</code> is in the <a href="#Method_sets">method set</a> of type <code>
T</code>, |
| 2544 <code>T.M</code> is a function that is callable as a regular function |
| 2545 with the same arguments as <code>M</code> prefixed by an additional |
| 2546 argument that is the receiver of the method. |
| 2547 </p> |
| 2548 |
| 2549 <pre class="ebnf"> |
| 2550 MethodExpr = ReceiverType "." MethodName . |
| 2551 ReceiverType = TypeName | "(" "*" TypeName ")" | "(" ReceiverType ")" . |
| 2552 </pre> |
| 2553 |
| 2554 <p> |
| 2555 Consider a struct type <code>T</code> with two methods, |
| 2556 <code>Mv</code>, whose receiver is of type <code>T</code>, and |
| 2557 <code>Mp</code>, whose receiver is of type <code>*T</code>. |
| 2558 </p> |
| 2559 |
| 2560 <pre> |
| 2561 type T struct { |
| 2562 a int |
| 2563 } |
| 2564 func (tv T) Mv(a int) int { return 0 } // value receiver |
| 2565 func (tp *T) Mp(f float32) float32 { return 1 } // pointer receiver |
| 2566 |
| 2567 var t T |
| 2568 </pre> |
| 2569 |
| 2570 <p> |
| 2571 The expression |
| 2572 </p> |
| 2573 |
| 2574 <pre> |
| 2575 T.Mv |
| 2576 </pre> |
| 2577 |
| 2578 <p> |
| 2579 yields a function equivalent to <code>Mv</code> but |
| 2580 with an explicit receiver as its first argument; it has signature |
| 2581 </p> |
| 2582 |
| 2583 <pre> |
| 2584 func(tv T, a int) int |
| 2585 </pre> |
| 2586 |
| 2587 <p> |
| 2588 That function may be called normally with an explicit receiver, so |
| 2589 these five invocations are equivalent: |
| 2590 </p> |
| 2591 |
| 2592 <pre> |
| 2593 t.Mv(7) |
| 2594 T.Mv(t, 7) |
| 2595 (T).Mv(t, 7) |
| 2596 f1 := T.Mv; f1(t, 7) |
| 2597 f2 := (T).Mv; f2(t, 7) |
| 2598 </pre> |
| 2599 |
| 2600 <p> |
| 2601 Similarly, the expression |
| 2602 </p> |
| 2603 |
| 2604 <pre> |
| 2605 (*T).Mp |
| 2606 </pre> |
| 2607 |
| 2608 <p> |
| 2609 yields a function value representing <code>Mp</code> with signature |
| 2610 </p> |
| 2611 |
| 2612 <pre> |
| 2613 func(tp *T, f float32) float32 |
| 2614 </pre> |
| 2615 |
| 2616 <p> |
| 2617 For a method with a value receiver, one can derive a function |
| 2618 with an explicit pointer receiver, so |
| 2619 </p> |
| 2620 |
| 2621 <pre> |
| 2622 (*T).Mv |
| 2623 </pre> |
| 2624 |
| 2625 <p> |
| 2626 yields a function value representing <code>Mv</code> with signature |
| 2627 </p> |
| 2628 |
| 2629 <pre> |
| 2630 func(tv *T, a int) int |
| 2631 </pre> |
| 2632 |
| 2633 <p> |
| 2634 Such a function indirects through the receiver to create a value |
| 2635 to pass as the receiver to the underlying method; |
| 2636 the method does not overwrite the value whose address is passed in |
| 2637 the function call. |
| 2638 </p> |
| 2639 |
| 2640 <p> |
| 2641 The final case, a value-receiver function for a pointer-receiver method, |
| 2642 is illegal because pointer-receiver methods are not in the method set |
| 2643 of the value type. |
| 2644 </p> |
| 2645 |
| 2646 <p> |
| 2647 Function values derived from methods are called with function call syntax; |
| 2648 the receiver is provided as the first argument to the call. |
| 2649 That is, given <code>f := T.Mv</code>, <code>f</code> is invoked |
| 2650 as <code>f(t, 7)</code> not <code>t.f(7)</code>. |
| 2651 To construct a function that binds the receiver, use a |
| 2652 <a href="#Function_literals">function literal</a> or |
| 2653 <a href="#Method_values">method value</a>. |
| 2654 </p> |
| 2655 |
| 2656 <p> |
| 2657 It is legal to derive a function value from a method of an interface type. |
| 2658 The resulting function takes an explicit receiver of that interface type. |
| 2659 </p> |
| 2660 |
| 2661 <h3 id="Method_values">Method values</h3> |
| 2662 |
| 2663 <p> |
| 2664 If the expression <code>x</code> has static type <code>T</code> and |
| 2665 <code>M</code> is in the <a href="#Method_sets">method set</a> of type <code>T</
code>, |
| 2666 <code>x.M</code> is called a <i>method value</i>. |
| 2667 The method value <code>x.M</code> is a function value that is callable |
| 2668 with the same arguments as a method call of <code>x.M</code>. |
| 2669 The expression <code>x</code> is evaluated and saved during the evaluation of th
e |
| 2670 method value; the saved copy is then used as the receiver in any calls, |
| 2671 which may be executed later. |
| 2672 </p> |
| 2673 |
| 2674 <p> |
| 2675 The type <code>T</code> may be an interface or non-interface type. |
| 2676 </p> |
| 2677 |
| 2678 <p> |
| 2679 As in the discussion of <a href="#Method_expressions">method expressions</a> abo
ve, |
| 2680 consider a struct type <code>T</code> with two methods, |
| 2681 <code>Mv</code>, whose receiver is of type <code>T</code>, and |
| 2682 <code>Mp</code>, whose receiver is of type <code>*T</code>. |
| 2683 </p> |
| 2684 |
| 2685 <pre> |
| 2686 type T struct { |
| 2687 a int |
| 2688 } |
| 2689 func (tv T) Mv(a int) int { return 0 } // value receiver |
| 2690 func (tp *T) Mp(f float32) float32 { return 1 } // pointer receiver |
| 2691 |
| 2692 var t T |
| 2693 var pt *T |
| 2694 func makeT() T |
| 2695 </pre> |
| 2696 |
| 2697 <p> |
| 2698 The expression |
| 2699 </p> |
| 2700 |
| 2701 <pre> |
| 2702 t.Mv |
| 2703 </pre> |
| 2704 |
| 2705 <p> |
| 2706 yields a function value of type |
| 2707 </p> |
| 2708 |
| 2709 <pre> |
| 2710 func(int) int |
| 2711 </pre> |
| 2712 |
| 2713 <p> |
| 2714 These two invocations are equivalent: |
| 2715 </p> |
| 2716 |
| 2717 <pre> |
| 2718 t.Mv(7) |
| 2719 f := t.Mv; f(7) |
| 2720 </pre> |
| 2721 |
| 2722 <p> |
| 2723 Similarly, the expression |
| 2724 </p> |
| 2725 |
| 2726 <pre> |
| 2727 pt.Mp |
| 2728 </pre> |
| 2729 |
| 2730 <p> |
| 2731 yields a function value of type |
| 2732 </p> |
| 2733 |
| 2734 <pre> |
| 2735 func(float32) float32 |
| 2736 </pre> |
| 2737 |
| 2738 <p> |
| 2739 As with <a href="#Selectors">selectors</a>, a reference to a non-interface metho
d with a value receiver |
| 2740 using a pointer will automatically dereference that pointer: <code>pt.Mv</code>
is equivalent to <code>(*pt).Mv</code>. |
| 2741 </p> |
| 2742 |
| 2743 <p> |
| 2744 As with <a href="#Calls">method calls</a>, a reference to a non-interface method
with a pointer receiver |
| 2745 using an addressable value will automatically take the address of that value: <c
ode>t.Mp</code> is equivalent to <code>(&t).Mp</code>. |
| 2746 </p> |
| 2747 |
| 2748 <pre> |
| 2749 f := t.Mv; f(7) // like t.Mv(7) |
| 2750 f := pt.Mp; f(7) // like pt.Mp(7) |
| 2751 f := pt.Mv; f(7) // like (*pt).Mv(7) |
| 2752 f := t.Mp; f(7) // like (&t).Mp(7) |
| 2753 f := makeT().Mp // invalid: result of makeT() is not addressable |
| 2754 </pre> |
| 2755 |
| 2756 <p> |
| 2757 Although the examples above use non-interface types, it is also legal to create
a method value |
| 2758 from a value of interface type. |
| 2759 </p> |
| 2760 |
| 2761 <pre> |
| 2762 var i interface { M(int) } = myVal |
| 2763 f := i.M; f(7) // like i.M(7) |
| 2764 </pre> |
| 2765 |
| 2766 |
2540 <h3 id="Index_expressions">Index expressions</h3> | 2767 <h3 id="Index_expressions">Index expressions</h3> |
2541 | 2768 |
2542 <p> | 2769 <p> |
2543 A primary expression of the form | 2770 A primary expression of the form |
2544 </p> | 2771 </p> |
2545 | 2772 |
2546 <pre> | 2773 <pre> |
2547 a[x] | 2774 a[x] |
2548 </pre> | 2775 </pre> |
2549 | 2776 |
(...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3364 <p> | 3591 <p> |
3365 For an operand <code>x</code> of type <code>T</code>, the address operation | 3592 For an operand <code>x</code> of type <code>T</code>, the address operation |
3366 <code>&x</code> generates a pointer of type <code>*T</code> to <code>x</code
>. | 3593 <code>&x</code> generates a pointer of type <code>*T</code> to <code>x</code
>. |
3367 The operand must be <i>addressable</i>, | 3594 The operand must be <i>addressable</i>, |
3368 that is, either a variable, pointer indirection, or slice indexing | 3595 that is, either a variable, pointer indirection, or slice indexing |
3369 operation; or a field selector of an addressable struct operand; | 3596 operation; or a field selector of an addressable struct operand; |
3370 or an array indexing operation of an addressable array. | 3597 or an array indexing operation of an addressable array. |
3371 As an exception to the addressability requirement, <code>x</code> may also be a | 3598 As an exception to the addressability requirement, <code>x</code> may also be a |
3372 (possibly parenthesized) | 3599 (possibly parenthesized) |
3373 <a href="#Composite_literals">composite literal</a>. | 3600 <a href="#Composite_literals">composite literal</a>. |
3374 If the evaluation of <code>x</code> would cause a <a href="#Run_time_panics">run
-time panic</a>, | 3601 If the evaluation of <code>x</code> would cause a <a href="#Run_time_panics">run
-time panic</a>, |
3375 then the evaluation of <code>&x</code> does too. | 3602 then the evaluation of <code>&x</code> does too. |
3376 </p> | 3603 </p> |
3377 | 3604 |
3378 <p> | 3605 <p> |
3379 For an operand <code>x</code> of pointer type <code>*T</code>, the pointer | 3606 For an operand <code>x</code> of pointer type <code>*T</code>, the pointer |
3380 indirection <code>*x</code> denotes the value of type <code>T</code> pointed | 3607 indirection <code>*x</code> denotes the value of type <code>T</code> pointed |
3381 to by <code>x</code>. | 3608 to by <code>x</code>. |
3382 If <code>x</code> is <code>nil</code>, an attempt to evaluate <code>*x</code> | 3609 If <code>x</code> is <code>nil</code>, an attempt to evaluate <code>*x</code> |
3383 will cause a <a href="#Run_time_panics">run-time panic</a>. | 3610 will cause a <a href="#Run_time_panics">run-time panic</a>. |
3384 </p> | 3611 </p> |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3428 </pre> | 3655 </pre> |
3429 | 3656 |
3430 <p> | 3657 <p> |
3431 yields an additional untyped boolean result reporting whether the | 3658 yields an additional untyped boolean result reporting whether the |
3432 communication succeeded. The value of <code>ok</code> is <code>true</code> | 3659 communication succeeded. The value of <code>ok</code> is <code>true</code> |
3433 if the value received was delivered by a successful send operation to the | 3660 if the value received was delivered by a successful send operation to the |
3434 channel, or <code>false</code> if it is a zero value generated because the | 3661 channel, or <code>false</code> if it is a zero value generated because the |
3435 channel is closed and empty. | 3662 channel is closed and empty. |
3436 </p> | 3663 </p> |
3437 | 3664 |
3438 | |
3439 <h3 id="Method_expressions">Method expressions</h3> | |
3440 | |
3441 <p> | |
3442 If <code>M</code> is in the <a href="#Method_sets">method set</a> of type <code>
T</code>, | |
3443 <code>T.M</code> is a function that is callable as a regular function | |
3444 with the same arguments as <code>M</code> prefixed by an additional | |
3445 argument that is the receiver of the method. | |
3446 </p> | |
3447 | |
3448 <pre class="ebnf"> | |
3449 MethodExpr = ReceiverType "." MethodName . | |
3450 ReceiverType = TypeName | "(" "*" TypeName ")" | "(" ReceiverType ")" . | |
3451 </pre> | |
3452 | |
3453 <p> | |
3454 Consider a struct type <code>T</code> with two methods, | |
3455 <code>Mv</code>, whose receiver is of type <code>T</code>, and | |
3456 <code>Mp</code>, whose receiver is of type <code>*T</code>. | |
3457 </p> | |
3458 | |
3459 <pre> | |
3460 type T struct { | |
3461 a int | |
3462 } | |
3463 func (tv T) Mv(a int) int { return 0 } // value receiver | |
3464 func (tp *T) Mp(f float32) float32 { return 1 } // pointer receiver | |
3465 | |
3466 var t T | |
3467 </pre> | |
3468 | |
3469 <p> | |
3470 The expression | |
3471 </p> | |
3472 | |
3473 <pre> | |
3474 T.Mv | |
3475 </pre> | |
3476 | |
3477 <p> | |
3478 yields a function equivalent to <code>Mv</code> but | |
3479 with an explicit receiver as its first argument; it has signature | |
3480 </p> | |
3481 | |
3482 <pre> | |
3483 func(tv T, a int) int | |
3484 </pre> | |
3485 | |
3486 <p> | |
3487 That function may be called normally with an explicit receiver, so | |
3488 these five invocations are equivalent: | |
3489 </p> | |
3490 | |
3491 <pre> | |
3492 t.Mv(7) | |
3493 T.Mv(t, 7) | |
3494 (T).Mv(t, 7) | |
3495 f1 := T.Mv; f1(t, 7) | |
3496 f2 := (T).Mv; f2(t, 7) | |
3497 </pre> | |
3498 | |
3499 <p> | |
3500 Similarly, the expression | |
3501 </p> | |
3502 | |
3503 <pre> | |
3504 (*T).Mp | |
3505 </pre> | |
3506 | |
3507 <p> | |
3508 yields a function value representing <code>Mp</code> with signature | |
3509 </p> | |
3510 | |
3511 <pre> | |
3512 func(tp *T, f float32) float32 | |
3513 </pre> | |
3514 | |
3515 <p> | |
3516 For a method with a value receiver, one can derive a function | |
3517 with an explicit pointer receiver, so | |
3518 </p> | |
3519 | |
3520 <pre> | |
3521 (*T).Mv | |
3522 </pre> | |
3523 | |
3524 <p> | |
3525 yields a function value representing <code>Mv</code> with signature | |
3526 </p> | |
3527 | |
3528 <pre> | |
3529 func(tv *T, a int) int | |
3530 </pre> | |
3531 | |
3532 <p> | |
3533 Such a function indirects through the receiver to create a value | |
3534 to pass as the receiver to the underlying method; | |
3535 the method does not overwrite the value whose address is passed in | |
3536 the function call. | |
3537 </p> | |
3538 | |
3539 <p> | |
3540 The final case, a value-receiver function for a pointer-receiver method, | |
3541 is illegal because pointer-receiver methods are not in the method set | |
3542 of the value type. | |
3543 </p> | |
3544 | |
3545 <p> | |
3546 Function values derived from methods are called with function call syntax; | |
3547 the receiver is provided as the first argument to the call. | |
3548 That is, given <code>f := T.Mv</code>, <code>f</code> is invoked | |
3549 as <code>f(t, 7)</code> not <code>t.f(7)</code>. | |
3550 To construct a function that binds the receiver, use a | |
3551 <a href="#Function_literals">function literal</a> or | |
3552 <a href="#Method_values">method value</a>. | |
3553 </p> | |
3554 | |
3555 <p> | |
3556 It is legal to derive a function value from a method of an interface type. | |
3557 The resulting function takes an explicit receiver of that interface type. | |
3558 </p> | |
3559 | |
3560 <h3 id="Method_values">Method values</h3> | |
3561 | |
3562 <p> | |
3563 If the expression <code>x</code> has static type <code>T</code> and | |
3564 <code>M</code> is in the <a href="#Method_sets">method set</a> of type <code>T</
code>, | |
3565 <code>x.M</code> is called a <i>method value</i>. | |
3566 The method value <code>x.M</code> is a function value that is callable | |
3567 with the same arguments as a method call of <code>x.M</code>. | |
3568 The expression <code>x</code> is evaluated and saved during the evaluation of th
e | |
3569 method value; the saved copy is then used as the receiver in any calls, | |
3570 which may be executed later. | |
3571 </p> | |
3572 | |
3573 <p> | |
3574 The type <code>T</code> may be an interface or non-interface type. | |
3575 </p> | |
3576 | |
3577 <p> | |
3578 As in the discussion of <a href="#Method_expressions">method expressions</a> abo
ve, | |
3579 consider a struct type <code>T</code> with two methods, | |
3580 <code>Mv</code>, whose receiver is of type <code>T</code>, and | |
3581 <code>Mp</code>, whose receiver is of type <code>*T</code>. | |
3582 </p> | |
3583 | |
3584 <pre> | |
3585 type T struct { | |
3586 a int | |
3587 } | |
3588 func (tv T) Mv(a int) int { return 0 } // value receiver | |
3589 func (tp *T) Mp(f float32) float32 { return 1 } // pointer receiver | |
3590 | |
3591 var t T | |
3592 var pt *T | |
3593 func makeT() T | |
3594 </pre> | |
3595 | |
3596 <p> | |
3597 The expression | |
3598 </p> | |
3599 | |
3600 <pre> | |
3601 t.Mv | |
3602 </pre> | |
3603 | |
3604 <p> | |
3605 yields a function value of type | |
3606 </p> | |
3607 | |
3608 <pre> | |
3609 func(int) int | |
3610 </pre> | |
3611 | |
3612 <p> | |
3613 These two invocations are equivalent: | |
3614 </p> | |
3615 | |
3616 <pre> | |
3617 t.Mv(7) | |
3618 f := t.Mv; f(7) | |
3619 </pre> | |
3620 | |
3621 <p> | |
3622 Similarly, the expression | |
3623 </p> | |
3624 | |
3625 <pre> | |
3626 pt.Mp | |
3627 </pre> | |
3628 | |
3629 <p> | |
3630 yields a function value of type | |
3631 </p> | |
3632 | |
3633 <pre> | |
3634 func(float32) float32 | |
3635 </pre> | |
3636 | |
3637 <p> | |
3638 As with <a href="#Selectors">selectors</a>, a reference to a non-interface metho
d with a value receiver | |
3639 using a pointer will automatically dereference that pointer: <code>pt.Mv</code>
is equivalent to <code>(*pt).Mv</code>. | |
3640 </p> | |
3641 | |
3642 <p> | |
3643 As with <a href="#Calls">method calls</a>, a reference to a non-interface method
with a pointer receiver | |
3644 using an addressable value will automatically take the address of that value: <c
ode>t.Mp</code> is equivalent to <code>(&t).Mp</code>. | |
3645 </p> | |
3646 | |
3647 <pre> | |
3648 f := t.Mv; f(7) // like t.Mv(7) | |
3649 f := pt.Mp; f(7) // like pt.Mp(7) | |
3650 f := pt.Mv; f(7) // like (*pt).Mv(7) | |
3651 f := t.Mp; f(7) // like (&t).Mp(7) | |
3652 f := makeT().Mp // invalid: result of makeT() is not addressable | |
3653 </pre> | |
3654 | |
3655 <p> | |
3656 Although the examples above use non-interface types, it is also legal to create
a method value | |
3657 from a value of interface type. | |
3658 </p> | |
3659 | |
3660 <pre> | |
3661 var i interface { M(int) } = myVal | |
3662 f := i.M; f(7) // like i.M(7) | |
3663 </pre> | |
3664 | 3665 |
3665 <h3 id="Conversions">Conversions</h3> | 3666 <h3 id="Conversions">Conversions</h3> |
3666 | 3667 |
3667 <p> | 3668 <p> |
3668 Conversions are expressions of the form <code>T(x)</code> | 3669 Conversions are expressions of the form <code>T(x)</code> |
3669 where <code>T</code> is a type and <code>x</code> is an expression | 3670 where <code>T</code> is a type and <code>x</code> is an expression |
3670 that can be converted to type <code>T</code>. | 3671 that can be converted to type <code>T</code>. |
3671 </p> | 3672 </p> |
3672 | 3673 |
3673 <pre class="ebnf"> | 3674 <pre class="ebnf"> |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4044 a := 1 | 4045 a := 1 |
4045 f := func() int { a++; return a } | 4046 f := func() int { a++; return a } |
4046 x := []int{a, f()} // x may be [1, 2] or [2, 2]: evaluation order bet
ween a and f() is not specified | 4047 x := []int{a, f()} // x may be [1, 2] or [2, 2]: evaluation order bet
ween a and f() is not specified |
4047 m := map[int]int{a: 1, a: 2} // m may be {2: 1} or {2: 2}: evaluation order bet
ween the two map assignments is not specified | 4048 m := map[int]int{a: 1, a: 2} // m may be {2: 1} or {2: 2}: evaluation order bet
ween the two map assignments is not specified |
4048 n := map[int]int{a: f()} // n may be {2: 3} or {3: 3}: evaluation order bet
ween the key and the value is not specified | 4049 n := map[int]int{a: f()} // n may be {2: 3} or {3: 3}: evaluation order bet
ween the key and the value is not specified |
4049 </pre> | 4050 </pre> |
4050 | 4051 |
4051 <p> | 4052 <p> |
4052 At package level, initialization dependencies override the left-to-right rule | 4053 At package level, initialization dependencies override the left-to-right rule |
4053 for individual initialization expressions, but not for operands within each | 4054 for individual initialization expressions, but not for operands within each |
4054 expression: | 4055 expression: |
4055 </p> | 4056 </p> |
4056 | 4057 |
4057 <pre> | 4058 <pre> |
4058 var a, b, c = f() + v(), g(), sqr(u()) + v() | 4059 var a, b, c = f() + v(), g(), sqr(u()) + v() |
4059 | 4060 |
4060 func f() int { return c } | 4061 func f() int { return c } |
4061 func g() int { return a } | 4062 func g() int { return a } |
4062 func sqr(x int) int { return x*x } | 4063 func sqr(x int) int { return x*x } |
4063 | 4064 |
4064 // functions u and v are independent of all other variables and functions | 4065 // functions u and v are independent of all other variables and functions |
(...skipping 1869 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5934 | 5935 |
5935 <ul> | 5936 <ul> |
5936 <li> | 5937 <li> |
5937 A reference to a variable or function is an identifier denoting that | 5938 A reference to a variable or function is an identifier denoting that |
5938 variable or function. | 5939 variable or function. |
5939 </li> | 5940 </li> |
5940 | 5941 |
5941 <li> | 5942 <li> |
5942 A reference to a method <code>m</code> is a | 5943 A reference to a method <code>m</code> is a |
5943 <a href="#Method_values">method value</a> or | 5944 <a href="#Method_values">method value</a> or |
5944 <a href="#Method_expressions">method expression</a> of the form | 5945 <a href="#Method_expressions">method expression</a> of the form |
5945 <code>t.m</code>, where the (static) type of <code>t</code> is | 5946 <code>t.m</code>, where the (static) type of <code>t</code> is |
5946 not an interface type, and the method <code>m</code> is in the | 5947 not an interface type, and the method <code>m</code> is in the |
5947 <a href="#Method_sets">method set</a> of <code>t</code>. | 5948 <a href="#Method_sets">method set</a> of <code>t</code>. |
5948 It is immaterial whether the resulting function value | 5949 It is immaterial whether the resulting function value |
5949 <code>t.m</code> is invoked. | 5950 <code>t.m</code> is invoked. |
5950 </li> | 5951 </li> |
5951 | 5952 |
5952 <li> | 5953 <li> |
5953 A variable, function, or method <code>x</code> depends on a variable | 5954 A variable, function, or method <code>x</code> depends on a variable |
5954 <code>y</code> if <code>x</code>'s initialization expression or body | 5955 <code>y</code> if <code>x</code>'s initialization expression or body |
5955 (for functions and methods) contains a reference to <code>y</code> | 5956 (for functions and methods) contains a reference to <code>y</code> |
5956 or to a function or method that depends on <code>y</code>. | 5957 or to a function or method that depends on <code>y</code>. |
5957 </li> | 5958 </li> |
5958 </ul> | 5959 </ul> |
5959 | 5960 |
5960 <p> | 5961 <p> |
5961 Dependency analysis is performed per package; only references referring | 5962 Dependency analysis is performed per package; only references referring |
5962 to variables, functions, and methods declared in the current package | 5963 to variables, functions, and methods declared in the current package |
5963 are considered. | 5964 are considered. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5995 <p> | 5996 <p> |
5996 Variables may also be initialized using functions named <code>init</code> | 5997 Variables may also be initialized using functions named <code>init</code> |
5997 declared in the package block, with no arguments and no result parameters. | 5998 declared in the package block, with no arguments and no result parameters. |
5998 </p> | 5999 </p> |
5999 | 6000 |
6000 <pre> | 6001 <pre> |
6001 func init() { … } | 6002 func init() { … } |
6002 </pre> | 6003 </pre> |
6003 | 6004 |
6004 <p> | 6005 <p> |
6005 Multiple such functions may be defined, even within a single | 6006 Multiple such functions may be defined, even within a single |
6006 source file. The <code>init</code> identifier is not | 6007 source file. The <code>init</code> identifier is not |
6007 <a href="#Declarations_and_scope">declared</a> and thus | 6008 <a href="#Declarations_and_scope">declared</a> and thus |
6008 <code>init</code> functions cannot be referred to from anywhere | 6009 <code>init</code> functions cannot be referred to from anywhere |
6009 in a program. | 6010 in a program. |
6010 </p> | 6011 </p> |
6011 | 6012 |
6012 <p> | 6013 <p> |
6013 A package with no imports is initialized by assigning initial values | 6014 A package with no imports is initialized by assigning initial values |
6014 to all its package-level variables followed by calling all <code>init</code> | 6015 to all its package-level variables followed by calling all <code>init</code> |
6015 functions in the order they appear in the source, possibly in multiple files, | 6016 functions in the order they appear in the source, possibly in multiple files, |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6202 </li> | 6203 </li> |
6203 | 6204 |
6204 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code>
is the same as | 6205 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code>
is the same as |
6205 <code>unsafe.Alignof(x[0])</code>, but at least 1. | 6206 <code>unsafe.Alignof(x[0])</code>, but at least 1. |
6206 </li> | 6207 </li> |
6207 </ol> | 6208 </ol> |
6208 | 6209 |
6209 <p> | 6210 <p> |
6210 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. | 6211 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. |
6211 </p> | 6212 </p> |
LEFT | RIGHT |