Files
learngo/interfaces/08-composition/book.go
2019-08-27 15:23:36 +03:00

42 lines
654 B
Go

// 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 (
"fmt"
"strconv"
"time"
)
type book struct {
*product
published interface{}
}
func (b *book) String() string {
p := format(b.published)
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"
}
const layout = "2006/01"
u := time.Unix(int64(t), 0)
return u.Format(layout)
}