add: string interface
This commit is contained in:
50
interfaces/09-stringer/book.go
Normal file
50
interfaces/09-stringer/book.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// 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{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// book satisfies the fmt.Stringer
|
||||||
|
func (b *book) String() string {
|
||||||
|
p := format(b.published)
|
||||||
|
|
||||||
|
// product.String has a pointer receiver.
|
||||||
|
// that's why we need to take its address here.
|
||||||
|
//
|
||||||
|
// when you put a value in an interface, the interface
|
||||||
|
// cannot run that value's type's value-receiver methods.
|
||||||
|
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)
|
||||||
|
}
|
14
interfaces/09-stringer/game.go
Normal file
14
interfaces/09-stringer/game.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
type game struct {
|
||||||
|
// game satisfies the fmt.Stringer
|
||||||
|
// because the product satisfies the fmt.Stringer
|
||||||
|
product
|
||||||
|
}
|
47
interfaces/09-stringer/list.go
Normal file
47
interfaces/09-stringer/list.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// 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"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type list []fmt.Stringer
|
||||||
|
|
||||||
|
// list satisfies the fmt.Stringer
|
||||||
|
func (l list) String() string {
|
||||||
|
if len(l) == 0 {
|
||||||
|
return "Sorry. We're waiting for delivery 🚚."
|
||||||
|
}
|
||||||
|
|
||||||
|
// use strings.Builder when you're combining unknown
|
||||||
|
// list of strings together.
|
||||||
|
var str strings.Builder
|
||||||
|
|
||||||
|
for _, it := range l {
|
||||||
|
// the builder doesn't know about the stringer interface.
|
||||||
|
// that's why you need to call String method here.
|
||||||
|
str.WriteString(it.String())
|
||||||
|
str.WriteRune('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
return str.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l list) discount(ratio float64) {
|
||||||
|
type discounter interface {
|
||||||
|
discount(float64)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, it := range l {
|
||||||
|
if it, ok := it.(discounter); ok {
|
||||||
|
it.discount(ratio)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
29
interfaces/09-stringer/main.go
Normal file
29
interfaces/09-stringer/main.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// 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"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
store := list{
|
||||||
|
&book{product{"moby dick", 10}, 118281600},
|
||||||
|
&book{product{"odyssey", 15}, "733622400"},
|
||||||
|
&book{product{"hobbit", 25}, nil},
|
||||||
|
&puzzle{product{"rubik's cube", 5}},
|
||||||
|
&game{product{"minecraft", 20}},
|
||||||
|
&game{product{"tetris", 5}},
|
||||||
|
&toy{product{"yoda", 150}},
|
||||||
|
}
|
||||||
|
|
||||||
|
store.discount(.5)
|
||||||
|
|
||||||
|
// the store doesn't have a print method anymore.
|
||||||
|
// but the Print function can print it.
|
||||||
|
// it's because, the list satisfies the stringer.
|
||||||
|
fmt.Print(store)
|
||||||
|
}
|
17
interfaces/09-stringer/money.go
Normal file
17
interfaces/09-stringer/money.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// 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"
|
||||||
|
|
||||||
|
type money float64
|
||||||
|
|
||||||
|
// money satisfies the fmt.Stringer
|
||||||
|
func (m money) String() string {
|
||||||
|
return fmt.Sprintf("$%.2f", m)
|
||||||
|
}
|
24
interfaces/09-stringer/product.go
Normal file
24
interfaces/09-stringer/product.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// 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"
|
||||||
|
|
||||||
|
type product struct {
|
||||||
|
title string
|
||||||
|
price money
|
||||||
|
}
|
||||||
|
|
||||||
|
// product satisfies the fmt.Stringer
|
||||||
|
func (p *product) String() string {
|
||||||
|
return fmt.Sprintf("%-15s: %s", p.title, p.price)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) discount(ratio float64) {
|
||||||
|
p.price *= money(1 - ratio)
|
||||||
|
}
|
14
interfaces/09-stringer/puzzle.go
Normal file
14
interfaces/09-stringer/puzzle.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
type puzzle struct {
|
||||||
|
// game satisfies the fmt.Stringer
|
||||||
|
// because the product satisfies the fmt.Stringer
|
||||||
|
product
|
||||||
|
}
|
14
interfaces/09-stringer/toy.go
Normal file
14
interfaces/09-stringer/toy.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
type toy struct {
|
||||||
|
// game satisfies the fmt.Stringer
|
||||||
|
// because the product satisfies the fmt.Stringer
|
||||||
|
product
|
||||||
|
}
|
Reference in New Issue
Block a user