refactor: stringer

This commit is contained in:
Inanc Gumus
2019-10-17 20:23:45 +03:00
parent c062a46355
commit 47b24c7649
8 changed files with 41 additions and 19 deletions

View File

@ -8,7 +8,6 @@
package main package main
import ( import (
"fmt"
"strings" "strings"
) )
@ -21,7 +20,10 @@ func (l list) String() string {
var str strings.Builder var str strings.Builder
for _, p := range l { 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() return str.String()
} }

View File

@ -10,17 +10,14 @@ package main
import "fmt" import "fmt"
func main() { 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{ l := list{
{title: "moby dick", price: 10, released: toTimestamp(118281600)}, {title: "moby dick", price: 10, released: toTimestamp(118281600)},
{title: "odyssey", price: 15, released: toTimestamp("733622400")}, {title: "odyssey", price: 15, released: toTimestamp("733622400")},
{title: "hobbit", price: 25}, {title: "hobbit", price: 25},
} }
l.discount(.5)
// The list is a stringer. // The list is a stringer.
// The `fmt.Print` function can print the `l` // The `fmt.Print` function can print the `l`
// by calling `l`'s `String()` method. // by calling `l`'s `String()` method.
@ -29,4 +26,9 @@ func main() {
// detect whether a type is a Stringer: // detect whether a type is a Stringer:
// https://golang.org/src/fmt/print.go#L627 // https://golang.org/src/fmt/print.go#L627
fmt.Print(l) 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)
} }

View File

@ -11,6 +11,8 @@ import "fmt"
type money float64 type money float64
// String() returns a string representation of money.
// money is an fmt.Stringer.
func (m money) String() string { func (m money) String() string {
return fmt.Sprintf("$%.2f", m) return fmt.Sprintf("$%.2f", m)
} }

View File

@ -12,22 +12,26 @@ import (
"time" "time"
) )
// timestamp stores, formats and automatically prints a timestamp: it's a stringer. // timestamp stores, formats and automatically prints a timestamp.
type timestamp struct { 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 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 { func (ts timestamp) String() string {
if ts.IsZero() { if ts.IsZero() { // same as: ts.Time.IsZero()
return "unknown" return "unknown"
} }
// Mon Jan 2 15:04:05 -0700 MST 2006 // Mon Jan 2 15:04:05 -0700 MST 2006
const layout = "2006/01" 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) { func toTimestamp(v interface{}) (ts timestamp) {
var t int var t int

View File

@ -8,7 +8,6 @@
package main package main
import ( import (
"fmt"
"sort" "sort"
"strings" "strings"
) )
@ -23,9 +22,13 @@ func (l list) String() string {
} }
sort.Sort(l) sort.Sort(l)
var str strings.Builder var str strings.Builder
for _, p := range l { 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() return str.String()
} }

View File

@ -8,7 +8,6 @@
package main package main
import ( import (
"fmt"
"sort" "sort"
"strings" "strings"
) )
@ -21,9 +20,13 @@ func (l list) String() string {
} }
sort.Sort(l) sort.Sort(l)
var str strings.Builder var str strings.Builder
for _, p := range l { 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() return str.String()
} }

View File

@ -8,7 +8,6 @@
package main package main
import ( import (
"fmt"
"sort" "sort"
"strings" "strings"
) )
@ -21,9 +20,13 @@ func (l list) String() string {
} }
sort.Sort(l) sort.Sort(l)
var str strings.Builder var str strings.Builder
for _, p := range l { 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() return str.String()
} }

View File

@ -8,7 +8,6 @@
package main package main
import ( import (
"fmt"
"sort" "sort"
"strings" "strings"
) )
@ -21,9 +20,13 @@ func (l list) String() string {
} }
sort.Sort(l) sort.Sort(l)
var str strings.Builder var str strings.Builder
for _, p := range l { 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() return str.String()
} }