From fcf5a8a791d7be2c888d010bc32c8dd7ec8125fe Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Sun, 1 Sep 2019 21:57:52 +0300 Subject: [PATCH] add: string interface --- interfaces/09-stringer/book.go | 50 +++++++++++++++++++++++++++++++ interfaces/09-stringer/game.go | 14 +++++++++ interfaces/09-stringer/list.go | 47 +++++++++++++++++++++++++++++ interfaces/09-stringer/main.go | 29 ++++++++++++++++++ interfaces/09-stringer/money.go | 17 +++++++++++ interfaces/09-stringer/product.go | 24 +++++++++++++++ interfaces/09-stringer/puzzle.go | 14 +++++++++ interfaces/09-stringer/toy.go | 14 +++++++++ 8 files changed, 209 insertions(+) create mode 100644 interfaces/09-stringer/book.go create mode 100644 interfaces/09-stringer/game.go create mode 100644 interfaces/09-stringer/list.go create mode 100644 interfaces/09-stringer/main.go create mode 100644 interfaces/09-stringer/money.go create mode 100644 interfaces/09-stringer/product.go create mode 100644 interfaces/09-stringer/puzzle.go create mode 100644 interfaces/09-stringer/toy.go diff --git a/interfaces/09-stringer/book.go b/interfaces/09-stringer/book.go new file mode 100644 index 0000000..d3b26d4 --- /dev/null +++ b/interfaces/09-stringer/book.go @@ -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) +} diff --git a/interfaces/09-stringer/game.go b/interfaces/09-stringer/game.go new file mode 100644 index 0000000..686fa81 --- /dev/null +++ b/interfaces/09-stringer/game.go @@ -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 +} diff --git a/interfaces/09-stringer/list.go b/interfaces/09-stringer/list.go new file mode 100644 index 0000000..aef1569 --- /dev/null +++ b/interfaces/09-stringer/list.go @@ -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) + } + } +} diff --git a/interfaces/09-stringer/main.go b/interfaces/09-stringer/main.go new file mode 100644 index 0000000..0f0b61a --- /dev/null +++ b/interfaces/09-stringer/main.go @@ -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) +} diff --git a/interfaces/09-stringer/money.go b/interfaces/09-stringer/money.go new file mode 100644 index 0000000..a246529 --- /dev/null +++ b/interfaces/09-stringer/money.go @@ -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) +} diff --git a/interfaces/09-stringer/product.go b/interfaces/09-stringer/product.go new file mode 100644 index 0000000..ef28671 --- /dev/null +++ b/interfaces/09-stringer/product.go @@ -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) +} diff --git a/interfaces/09-stringer/puzzle.go b/interfaces/09-stringer/puzzle.go new file mode 100644 index 0000000..1a4a6d7 --- /dev/null +++ b/interfaces/09-stringer/puzzle.go @@ -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 +} diff --git a/interfaces/09-stringer/toy.go b/interfaces/09-stringer/toy.go new file mode 100644 index 0000000..9c630c8 --- /dev/null +++ b/interfaces/09-stringer/toy.go @@ -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 +}