From 47b24c7649e7370215faf51faa3b0427127d2f4b Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Thu, 17 Oct 2019 20:23:45 +0300 Subject: [PATCH] refactor: stringer --- interfaces/10-stringer/list.go | 6 ++++-- interfaces/10-stringer/main.go | 12 +++++++----- interfaces/10-stringer/money.go | 2 ++ interfaces/10-stringer/timestamp.go | 12 ++++++++---- interfaces/11-sort/list.go | 7 +++++-- interfaces/12-marshaler/list.go | 7 +++++-- interfaces/13-io/list.go | 7 +++++-- interfaces/14-composition/list.go | 7 +++++-- 8 files changed, 41 insertions(+), 19 deletions(-) diff --git a/interfaces/10-stringer/list.go b/interfaces/10-stringer/list.go index 84952d6..b920ec0 100644 --- a/interfaces/10-stringer/list.go +++ b/interfaces/10-stringer/list.go @@ -8,7 +8,6 @@ package main import ( - "fmt" "strings" ) @@ -21,7 +20,10 @@ func (l list) String() string { var str strings.Builder for _, p := range l { - fmt.Fprintf(&str, "* %s\n", p) + // fmt.Printf("* %s\n", p) + str.WriteString("* ") + str.WriteString(p.String()) + str.WriteRune('\n') } return str.String() } diff --git a/interfaces/10-stringer/main.go b/interfaces/10-stringer/main.go index f15eff2..be85bef 100644 --- a/interfaces/10-stringer/main.go +++ b/interfaces/10-stringer/main.go @@ -10,17 +10,14 @@ package main import "fmt" func main() { - // The money type is a stringer. - // You don't need to call the String method when printing a value of it. - // var pocket money = 10 - // fmt.Println(pocket) - l := list{ {title: "moby dick", price: 10, released: toTimestamp(118281600)}, {title: "odyssey", price: 15, released: toTimestamp("733622400")}, {title: "hobbit", price: 25}, } + l.discount(.5) + // The list is a stringer. // The `fmt.Print` function can print the `l` // by calling `l`'s `String()` method. @@ -29,4 +26,9 @@ func main() { // detect whether a type is a Stringer: // https://golang.org/src/fmt/print.go#L627 fmt.Print(l) + + // The money type is a stringer. + // You don't need to call the String method when printing a value of it. + // var pocket money = 10 + // fmt.Println("I have", pocket) } diff --git a/interfaces/10-stringer/money.go b/interfaces/10-stringer/money.go index 095a039..8e3c9e5 100644 --- a/interfaces/10-stringer/money.go +++ b/interfaces/10-stringer/money.go @@ -11,6 +11,8 @@ import "fmt" type money float64 +// String() returns a string representation of money. +// money is an fmt.Stringer. func (m money) String() string { return fmt.Sprintf("$%.2f", m) } diff --git a/interfaces/10-stringer/timestamp.go b/interfaces/10-stringer/timestamp.go index 6e43c4d..585ffed 100644 --- a/interfaces/10-stringer/timestamp.go +++ b/interfaces/10-stringer/timestamp.go @@ -12,22 +12,26 @@ import ( "time" ) -// timestamp stores, formats and automatically prints a timestamp: it's a stringer. +// timestamp stores, formats and automatically prints a timestamp. type timestamp struct { + // timestamp anonymously embeds a time. + // no need to convert a time value to a timestamp value to use the methods of the time type. time.Time } -// String method makes the timestamp an fmt.stringer. +// String() returns a string representation of timestamp. +// timestamp is an fmt.Stringer. func (ts timestamp) String() string { - if ts.IsZero() { + if ts.IsZero() { // same as: ts.Time.IsZero() return "unknown" } // Mon Jan 2 15:04:05 -0700 MST 2006 const layout = "2006/01" - return ts.Format(layout) + return ts.Format(layout) // same as: ts.Time.Format(layout) } +// toTimestamp returns a timestamp value depending on the type of `v`. func toTimestamp(v interface{}) (ts timestamp) { var t int diff --git a/interfaces/11-sort/list.go b/interfaces/11-sort/list.go index 49d3e4f..69a9a85 100644 --- a/interfaces/11-sort/list.go +++ b/interfaces/11-sort/list.go @@ -8,7 +8,6 @@ package main import ( - "fmt" "sort" "strings" ) @@ -23,9 +22,13 @@ func (l list) String() string { } sort.Sort(l) + var str strings.Builder for _, p := range l { - fmt.Fprintf(&str, "* %s\n", p) + // fmt.Printf("* %s\n", p) + str.WriteString("* ") + str.WriteString(p.String()) + str.WriteRune('\n') } return str.String() } diff --git a/interfaces/12-marshaler/list.go b/interfaces/12-marshaler/list.go index c0787f4..018b629 100644 --- a/interfaces/12-marshaler/list.go +++ b/interfaces/12-marshaler/list.go @@ -8,7 +8,6 @@ package main import ( - "fmt" "sort" "strings" ) @@ -21,9 +20,13 @@ func (l list) String() string { } sort.Sort(l) + var str strings.Builder for _, p := range l { - fmt.Fprintf(&str, "* %s\n", p) + // fmt.Printf("* %s\n", p) + str.WriteString("* ") + str.WriteString(p.String()) + str.WriteRune('\n') } return str.String() } diff --git a/interfaces/13-io/list.go b/interfaces/13-io/list.go index c0787f4..018b629 100644 --- a/interfaces/13-io/list.go +++ b/interfaces/13-io/list.go @@ -8,7 +8,6 @@ package main import ( - "fmt" "sort" "strings" ) @@ -21,9 +20,13 @@ func (l list) String() string { } sort.Sort(l) + var str strings.Builder for _, p := range l { - fmt.Fprintf(&str, "* %s\n", p) + // fmt.Printf("* %s\n", p) + str.WriteString("* ") + str.WriteString(p.String()) + str.WriteRune('\n') } return str.String() } diff --git a/interfaces/14-composition/list.go b/interfaces/14-composition/list.go index c0787f4..018b629 100644 --- a/interfaces/14-composition/list.go +++ b/interfaces/14-composition/list.go @@ -8,7 +8,6 @@ package main import ( - "fmt" "sort" "strings" ) @@ -21,9 +20,13 @@ func (l list) String() string { } sort.Sort(l) + var str strings.Builder for _, p := range l { - fmt.Fprintf(&str, "* %s\n", p) + // fmt.Printf("* %s\n", p) + str.WriteString("* ") + str.WriteString(p.String()) + str.WriteRune('\n') } return str.String() }