From: Rob Pike sort.IntArray to reduce the entire example
+type sort.IntSlice to reduce the entire example
to this:
@@ -1879,14 +1879,14 @@ type Sequence []int
// Method for printing - sorts the elements before printing
func (s Sequence) String() string {
- sort.IntArray(s).Sort()
+ sort.IntSlice(s).Sort()
return fmt.Sprint([]int(s))
}
Now, instead of having Sequence implement multiple
interfaces (sorting and printing), we're using the ability of a data item to be
-converted to multiple types (Sequence, sort.IntArray
+converted to multiple types (Sequence, sort.IntSlice
and []int), each of which does some part of the job.
That's more unusual in practice but can be effective.
ArgServer now has same signature as HandlerFunc,
so it can be converted to that type to access its methods,
-just as we converted Sequence to IntArray
-to access IntArray.Sort.
+just as we converted Sequence to IntSlice
+to access IntSlice.Sort.
The code to set it up is concise:
diff --git a/doc/effective_go.tmpl b/doc/effective_go.tmpl index 46d774ad4e..da827368b1 100644 --- a/doc/effective_go.tmpl +++ b/doc/effective_go.tmpl @@ -1809,7 +1809,7 @@ do create a new value.) It's an idiom in Go programs to convert the type of an expression to access a different set of methods. As an example, we could use the existing -typesort.IntArrayto reduce the entire example +typesort.IntSliceto reduce the entire example to this:@@ -1817,14 +1817,14 @@ type Sequence []int // Method for printing - sorts the elements before printing func (s Sequence) String() string { - sort.IntArray(s).Sort() + sort.IntSlice(s).Sort() return fmt.Sprint([]int(s)) }Now, instead of having
@@ -2019,8 +2019,8 @@ func ArgServer(w http.ResponseWriter, req *http.Request) {Sequenceimplement multiple interfaces (sorting and printing), we're using the ability of a data item to be -converted to multiple types (Sequence,sort.IntArray+converted to multiple types (Sequence,sort.IntSliceand[]int), each of which does some part of the job. That's more unusual in practice but can be effective.
ArgServernow has same signature asHandlerFunc, so it can be converted to that type to access its methods, -just as we convertedSequencetoIntArray-to accessIntArray.Sort. +just as we convertedSequencetoIntSlice+to accessIntSlice.Sort. The code to set it up is concise: