mirror of
https://github.com/golang/go
synced 2024-11-21 20:14:52 -07:00
Effective Go: IntArray -> IntSlice
Fixes #2336. R=golang-dev, dsymonds, rsc CC=golang-dev https://golang.org/cl/5222042
This commit is contained in:
parent
417c42218f
commit
029c9bcb8b
@ -1871,7 +1871,7 @@ do create a new value.)
|
|||||||
It's an idiom in Go programs to convert the
|
It's an idiom in Go programs to convert the
|
||||||
type of an expression to access a different
|
type of an expression to access a different
|
||||||
set of methods. As an example, we could use the existing
|
set of methods. As an example, we could use the existing
|
||||||
type <code>sort.IntArray</code> to reduce the entire example
|
type <code>sort.IntSlice</code> to reduce the entire example
|
||||||
to this:
|
to this:
|
||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
@ -1879,14 +1879,14 @@ type Sequence []int
|
|||||||
|
|
||||||
// Method for printing - sorts the elements before printing
|
// Method for printing - sorts the elements before printing
|
||||||
func (s Sequence) String() string {
|
func (s Sequence) String() string {
|
||||||
sort.IntArray(s).Sort()
|
sort.IntSlice(s).Sort()
|
||||||
return fmt.Sprint([]int(s))
|
return fmt.Sprint([]int(s))
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Now, instead of having <code>Sequence</code> implement multiple
|
Now, instead of having <code>Sequence</code> implement multiple
|
||||||
interfaces (sorting and printing), we're using the ability of a data item to be
|
interfaces (sorting and printing), we're using the ability of a data item to be
|
||||||
converted to multiple types (<code>Sequence</code>, <code>sort.IntArray</code>
|
converted to multiple types (<code>Sequence</code>, <code>sort.IntSlice</code>
|
||||||
and <code>[]int</code>), each of which does some part of the job.
|
and <code>[]int</code>), each of which does some part of the job.
|
||||||
That's more unusual in practice but can be effective.
|
That's more unusual in practice but can be effective.
|
||||||
</p>
|
</p>
|
||||||
@ -2081,8 +2081,8 @@ func ArgServer(w http.ResponseWriter, req *http.Request) {
|
|||||||
<p>
|
<p>
|
||||||
<code>ArgServer</code> now has same signature as <code>HandlerFunc</code>,
|
<code>ArgServer</code> now has same signature as <code>HandlerFunc</code>,
|
||||||
so it can be converted to that type to access its methods,
|
so it can be converted to that type to access its methods,
|
||||||
just as we converted <code>Sequence</code> to <code>IntArray</code>
|
just as we converted <code>Sequence</code> to <code>IntSlice</code>
|
||||||
to access <code>IntArray.Sort</code>.
|
to access <code>IntSlice.Sort</code>.
|
||||||
The code to set it up is concise:
|
The code to set it up is concise:
|
||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
|
@ -1809,7 +1809,7 @@ do create a new value.)
|
|||||||
It's an idiom in Go programs to convert the
|
It's an idiom in Go programs to convert the
|
||||||
type of an expression to access a different
|
type of an expression to access a different
|
||||||
set of methods. As an example, we could use the existing
|
set of methods. As an example, we could use the existing
|
||||||
type <code>sort.IntArray</code> to reduce the entire example
|
type <code>sort.IntSlice</code> to reduce the entire example
|
||||||
to this:
|
to this:
|
||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
@ -1817,14 +1817,14 @@ type Sequence []int
|
|||||||
|
|
||||||
// Method for printing - sorts the elements before printing
|
// Method for printing - sorts the elements before printing
|
||||||
func (s Sequence) String() string {
|
func (s Sequence) String() string {
|
||||||
sort.IntArray(s).Sort()
|
sort.IntSlice(s).Sort()
|
||||||
return fmt.Sprint([]int(s))
|
return fmt.Sprint([]int(s))
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Now, instead of having <code>Sequence</code> implement multiple
|
Now, instead of having <code>Sequence</code> implement multiple
|
||||||
interfaces (sorting and printing), we're using the ability of a data item to be
|
interfaces (sorting and printing), we're using the ability of a data item to be
|
||||||
converted to multiple types (<code>Sequence</code>, <code>sort.IntArray</code>
|
converted to multiple types (<code>Sequence</code>, <code>sort.IntSlice</code>
|
||||||
and <code>[]int</code>), each of which does some part of the job.
|
and <code>[]int</code>), each of which does some part of the job.
|
||||||
That's more unusual in practice but can be effective.
|
That's more unusual in practice but can be effective.
|
||||||
</p>
|
</p>
|
||||||
@ -2019,8 +2019,8 @@ func ArgServer(w http.ResponseWriter, req *http.Request) {
|
|||||||
<p>
|
<p>
|
||||||
<code>ArgServer</code> now has same signature as <code>HandlerFunc</code>,
|
<code>ArgServer</code> now has same signature as <code>HandlerFunc</code>,
|
||||||
so it can be converted to that type to access its methods,
|
so it can be converted to that type to access its methods,
|
||||||
just as we converted <code>Sequence</code> to <code>IntArray</code>
|
just as we converted <code>Sequence</code> to <code>IntSlice</code>
|
||||||
to access <code>IntArray.Sort</code>.
|
to access <code>IntSlice.Sort</code>.
|
||||||
The code to set it up is concise:
|
The code to set it up is concise:
|
||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
|
Loading…
Reference in New Issue
Block a user