From 029c9bcb8bbf4b9dd55293d9b41fc1c16994b3f9 Mon Sep 17 00:00:00 2001
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 46d774ad4ef..da827368b1c 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.IntArray
to reduce the entire example +typesort.IntSlice
to 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) {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 asHandlerFunc
, so it can be converted to that type to access its methods, -just as we convertedSequence
toIntArray
-to accessIntArray.Sort
. +just as we convertedSequence
toIntSlice
+to accessIntSlice.Sort
. The code to set it up is concise: