refactor: stringer interface
This commit is contained in:
@ -9,43 +9,20 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type book struct {
|
||||
product
|
||||
published interface{}
|
||||
published timestamp
|
||||
}
|
||||
|
||||
// book satisfies the fmt.Stringer
|
||||
func (b *book) String() string {
|
||||
p := format(b.published)
|
||||
|
||||
// product.String() has a pointer receiver.
|
||||
// That's why you need to manually take the product's address here.
|
||||
//
|
||||
// If you pass: "b.product", Go would pass it as a copy to Sprintf.
|
||||
// In that case, Go can't deference b.product automatically.
|
||||
// It's because: b.product would be different value—a copy.
|
||||
return fmt.Sprintf("%s - (%v)", &b.product, p)
|
||||
}
|
||||
|
||||
func format(v interface{}) string {
|
||||
var t int
|
||||
|
||||
switch v := v.(type) {
|
||||
case int:
|
||||
t = v
|
||||
case string:
|
||||
t, _ = strconv.Atoi(v)
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// Mon Jan 2 15:04:05 -0700 MST 2006
|
||||
const layout = "2006/01"
|
||||
|
||||
u := time.Unix(int64(t), 0)
|
||||
return u.Format(layout)
|
||||
return fmt.Sprintf("%s - (%s)", &b.product, b.published)
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import "fmt"
|
||||
|
||||
func main() {
|
||||
store := list{
|
||||
&book{product{"moby dick", 10}, 118281600},
|
||||
&book{product{"odyssey", 15}, "733622400"},
|
||||
&book{product{"hobbit", 25}, nil},
|
||||
&book{product{"moby dick", 10}, toTimestamp(118281600)},
|
||||
&book{product{"odyssey", 15}, toTimestamp("733622400")},
|
||||
&book{product{"hobbit", 25}, unknown},
|
||||
&puzzle{product{"rubik's cube", 5}},
|
||||
&game{product{"minecraft", 20}},
|
||||
&game{product{"tetris", 5}},
|
||||
@ -26,4 +26,8 @@ func main() {
|
||||
// but the Print function can print it.
|
||||
// it's because, the list satisfies the stringer.
|
||||
fmt.Print(store)
|
||||
|
||||
// timestamp is useful even if it's zero.
|
||||
var ts timestamp
|
||||
fmt.Println(ts)
|
||||
}
|
||||
|
51
interfaces/09-stringer/timestamp.go
Normal file
51
interfaces/09-stringer/timestamp.go
Normal file
@ -0,0 +1,51 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Mon Jan 2 15:04:05 -0700 MST 2006
|
||||
const layout = "2006/01"
|
||||
|
||||
// unknown is the zero value of a timestamp.
|
||||
var unknown = timestamp(time.Time{})
|
||||
|
||||
// Timestamp prints timestamps, it's a stringer.
|
||||
// Timestamp is useful even if it's zero.
|
||||
type timestamp time.Time
|
||||
|
||||
// String makes the timestamp a stringer.
|
||||
func (ts timestamp) String() string {
|
||||
t := time.Time(ts)
|
||||
|
||||
if t.IsZero() {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
return t.Format(layout)
|
||||
}
|
||||
|
||||
// toTimestamp was "book.format" before.
|
||||
// Now it returns a timestamp value depending on the type of `v`.
|
||||
func toTimestamp(v interface{}) timestamp {
|
||||
var t int
|
||||
|
||||
switch v := v.(type) {
|
||||
case int:
|
||||
t = v
|
||||
case string:
|
||||
t, _ = strconv.Atoi(v)
|
||||
default:
|
||||
return unknown
|
||||
}
|
||||
|
||||
return timestamp(time.Unix(int64(t), 0))
|
||||
}
|
Reference in New Issue
Block a user