Left: | ||
Right: |
OLD | NEW |
---|---|
1 <!--{ | 1 <!--{ |
2 "Title": "Effective Go", | 2 "Title": "Effective Go", |
3 "Template": true | 3 "Template": true |
4 }--> | 4 }--> |
5 | 5 |
6 <h2 id="introduction">Introduction</h2> | 6 <h2 id="introduction">Introduction</h2> |
7 | 7 |
8 <p> | 8 <p> |
9 Go is a new language. Although it borrows ideas from | 9 Go is a new language. Although it borrows ideas from |
10 existing languages, | 10 existing languages, |
(...skipping 2038 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2049 </p> | 2049 </p> |
2050 <pre> | 2050 <pre> |
2051 var b ByteSlice | 2051 var b ByteSlice |
2052 fmt.Fprintf(&b, "This hour has %d days\n", 7) | 2052 fmt.Fprintf(&b, "This hour has %d days\n", 7) |
2053 </pre> | 2053 </pre> |
2054 <p> | 2054 <p> |
2055 We pass the address of a <code>ByteSlice</code> | 2055 We pass the address of a <code>ByteSlice</code> |
2056 because only <code>*ByteSlice</code> satisfies <code>io.Writer</code>. | 2056 because only <code>*ByteSlice</code> satisfies <code>io.Writer</code>. |
2057 The rule about pointers vs. values for receivers is that value methods | 2057 The rule about pointers vs. values for receivers is that value methods |
2058 can be invoked on pointers and values, but pointer methods can only be | 2058 can be invoked on pointers and values, but pointer methods can only be |
2059 invoked on pointers. This is because pointer methods can modify the | 2059 invoked on pointers. |
2060 receiver; invoking them on a copy of the value would cause those | |
2061 modifications to be discarded. | |
2062 </p> | 2060 </p> |
2061 | |
2062 <p> | |
2063 This rule arises because pointer methods can modify the receiver; invoking | |
2064 them on a value would cause the method to receive a copy of the value, so | |
2065 any modifications would be discarded. The language therefore disallows | |
r
2014/04/17 05:36:53
move the new sentence to its own line.
| |
2066 this mistake. | |
2067 There is a handy exception. When the value is addressable, the language | |
r
2014/04/17 05:36:53
s/exception/&, though/
| |
2068 takes care of the common case of invoking a pointer method on a value by | |
2069 inserting the address operator automatically. | |
2070 In our example, the variable <code>b</code> is addressable, so we can call | |
2071 its <code>Write</code> method with just <code>b.Write</code>. The compiler | |
2072 will rewrite that to <code>(&b).Write</code> for us. | |
2073 </p> | |
2074 | |
2063 <p> | 2075 <p> |
2064 By the way, the idea of using <code>Write</code> on a slice of bytes | 2076 By the way, the idea of using <code>Write</code> on a slice of bytes |
2065 is central to the implementation of <code>bytes.Buffer</code>. | 2077 is central to the implementation of <code>bytes.Buffer</code>. |
2066 </p> | 2078 </p> |
2067 | 2079 |
2068 <h2 id="interfaces_and_types">Interfaces and other types</h2> | 2080 <h2 id="interfaces_and_types">Interfaces and other types</h2> |
2069 | 2081 |
2070 <h3 id="interfaces">Interfaces</h3> | 2082 <h3 id="interfaces">Interfaces</h3> |
2071 <p> | 2083 <p> |
2072 Interfaces in Go provide a way to specify the behavior of an | 2084 Interfaces in Go provide a way to specify the behavior of an |
(...skipping 1556 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3629 <pre> | 3641 <pre> |
3630 verifying implementation | 3642 verifying implementation |
3631 type Color uint32 | 3643 type Color uint32 |
3632 | 3644 |
3633 // Check that Color implements image.Color and image.Image | 3645 // Check that Color implements image.Color and image.Image |
3634 var _ image.Color = Black | 3646 var _ image.Color = Black |
3635 var _ image.Image = Black | 3647 var _ image.Image = Black |
3636 </pre> | 3648 </pre> |
3637 --> | 3649 --> |
3638 | 3650 |
OLD | NEW |