refactor: stringer
This commit is contained in:
		@@ -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()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user