diff --git a/interfaces/11-sort/list.go b/interfaces/11-sort/list.go index 722b337..2e12a89 100644 --- a/interfaces/11-sort/list.go +++ b/interfaces/11-sort/list.go @@ -65,5 +65,5 @@ func (bp byRelease) Swap(i, j int) { bp.list.Swap(i, j) } */ func byReleaseDate(l list) sort.Interface { - return &byRelease{l} + return byRelease{l} } diff --git a/interfaces/11-sort/main.go b/interfaces/11-sort/main.go index 1ebb40d..fd47fb5 100644 --- a/interfaces/11-sort/main.go +++ b/interfaces/11-sort/main.go @@ -9,6 +9,7 @@ package main import ( "fmt" + "sort" ) func main() { @@ -18,10 +19,10 @@ func main() { {title: "hobbit", price: 25}, } - // sort.Sort(l) - // sort.Sort(sort.Reverse(l)) - // sort.Sort(byReleaseDate(l)) - // sort.Sort(sort.Reverse(byReleaseDate(l))) + sort.Sort(l) + sort.Sort(sort.Reverse(l)) + sort.Sort(byReleaseDate(l)) + sort.Sort(sort.Reverse(byReleaseDate(l))) fmt.Print(l) } @@ -33,13 +34,14 @@ Summary: - sort.Interface has three methods: Len(), Less(), Swap() - Len() returns the length of a collection. - - Less() should return true when an element comes before another one. - - Swap()s the elements when Less() returns true. + - Less(i, j) should return true when an element comes before another one. + - Swap(i, j)s the elements when Less() returns true. -- sort.Reverse() sorts a sort.Interface value. +- sort.Reverse() can reverse sort a type that satisfies the sort.Interface. - You can customize the sorting: - - by anonymously embedding the sort.Interface type + - Either by implementing the sort.Interface methods, + - or by anonymously embedding a type that already satisfies the sort.Interface - and adding a Less() method. - Anonymous embedding means auto-forwarding method calls to an embedded value. diff --git a/interfaces/12-marshaler/list.go b/interfaces/12-marshaler/list.go index e313b8f..8b09119 100644 --- a/interfaces/12-marshaler/list.go +++ b/interfaces/12-marshaler/list.go @@ -50,5 +50,5 @@ func (bp byRelease) Less(i, j int) bool { } func byReleaseDate(l list) sort.Interface { - return &byRelease{l} + return byRelease{l} } diff --git a/interfaces/12-marshaler/main.go b/interfaces/12-marshaler/main.go index a238fd9..1451b43 100644 --- a/interfaces/12-marshaler/main.go +++ b/interfaces/12-marshaler/main.go @@ -34,14 +34,12 @@ func main() { Summary: - json.Marshal() and json.MarshalIndent() can only encode primitive types. - - It cannot encode custom types. - - Implement the json.Marshaler interface and teach it how to encode your custom types. - - See time.Time code: It satisfies the json.Marshaler interface. + - Custom types can tell the encoder how to encode. + - To do that satisfy the json.Marshaler interface. - json.Unmarshal() can only decode primitive types. - - It cannot decode custom types. - - Implement the json.Unmarshaler interface and teach it how to decode your custom types. - - See time.Time code: It satisfies the json.Unmarshaler interface as well. + - Custom types can tell the decoder how to decode. + - To do that satisfy the json.Unmarshaler interface. - strconv.AppendInt() can append an int value to a []byte. - There are several other functions in the strconv package for other primitive types as well. diff --git a/interfaces/13-io/list.go b/interfaces/13-io/list.go index 94f5c04..fa60a0f 100644 --- a/interfaces/13-io/list.go +++ b/interfaces/13-io/list.go @@ -50,5 +50,5 @@ func (bp byRelease) Less(i, j int) bool { } func byReleaseDate(l list) sort.Interface { - return &byRelease{l} + return byRelease{l} } diff --git a/interfaces/14-composition/list.go b/interfaces/14-composition/list.go index 94f5c04..fa60a0f 100644 --- a/interfaces/14-composition/list.go +++ b/interfaces/14-composition/list.go @@ -50,5 +50,5 @@ func (bp byRelease) Less(i, j int) bool { } func byReleaseDate(l list) sort.Interface { - return &byRelease{l} + return byRelease{l} }