From 9bcaae2505bfcf303d1b30360ae15cc78c589b91 Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Sat, 31 Aug 2019 20:28:08 +0300 Subject: [PATCH] add: method embedding --- interfaces/08-method-embedding/book.go | 60 +++++++++++++++++++++++ interfaces/08-method-embedding/game.go | 12 +++++ interfaces/08-method-embedding/list.go | 39 +++++++++++++++ interfaces/08-method-embedding/main.go | 29 +++++++++++ interfaces/08-method-embedding/money.go | 16 ++++++ interfaces/08-method-embedding/product.go | 23 +++++++++ interfaces/08-method-embedding/puzzle.go | 12 +++++ interfaces/08-method-embedding/toy.go | 12 +++++ 8 files changed, 203 insertions(+) create mode 100644 interfaces/08-method-embedding/book.go create mode 100644 interfaces/08-method-embedding/game.go create mode 100644 interfaces/08-method-embedding/list.go create mode 100644 interfaces/08-method-embedding/main.go create mode 100644 interfaces/08-method-embedding/money.go create mode 100644 interfaces/08-method-embedding/product.go create mode 100644 interfaces/08-method-embedding/puzzle.go create mode 100644 interfaces/08-method-embedding/toy.go diff --git a/interfaces/08-method-embedding/book.go b/interfaces/08-method-embedding/book.go new file mode 100644 index 0000000..b3dfff0 --- /dev/null +++ b/interfaces/08-method-embedding/book.go @@ -0,0 +1,60 @@ +// 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 { + // embed the product type into the book type. + // all the fields and methods of the product will be + // available in this book type. + product + published interface{} +} + +// book type's print method takes priority. +// +// + when you call it on a book value, the following method will +// be executed. +// +// + if it wasn't here, the product type's print method would +// have been executed. +func (b *book) print() { + // the book can also call the embedded product's print method + // if it wants to, as in here: + b.product.print() + + p := format(b.published) + fmt.Printf("\t - (%v)\n", p) +} + +func format(v interface{}) string { + var t int + + switch v := v.(type) { + case int: + // book{title: "moby dick", price: 10, published: 118281600}, + t = v + case string: + // book{title: "odyssey", price: 15, published: "733622400"}, + t, _ = strconv.Atoi(v) + default: + // book{title: "hobbit", price: 25}, + 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/08-method-embedding/game.go b/interfaces/08-method-embedding/game.go new file mode 100644 index 0000000..3604b15 --- /dev/null +++ b/interfaces/08-method-embedding/game.go @@ -0,0 +1,12 @@ +// 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 { + product +} diff --git a/interfaces/08-method-embedding/list.go b/interfaces/08-method-embedding/list.go new file mode 100644 index 0000000..ec401e1 --- /dev/null +++ b/interfaces/08-method-embedding/list.go @@ -0,0 +1,39 @@ +// 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 printer interface { + print() +} + +type list []printer + +func (l list) print() { + if len(l) == 0 { + fmt.Println("Sorry. We're waiting for delivery 🚚.") + return + } + + for _, it := range l { + it.print() + } +} + +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/08-method-embedding/main.go b/interfaces/08-method-embedding/main.go new file mode 100644 index 0000000..48fed2c --- /dev/null +++ b/interfaces/08-method-embedding/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 + +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) + store.print() + + // t := &toy{product{"yoda", 150}} + // fmt.Printf("%#v\n", t) + + // b := &book{product{"moby dick", 10}, 118281600} + // fmt.Printf("%#v\n", b) +} diff --git a/interfaces/08-method-embedding/money.go b/interfaces/08-method-embedding/money.go new file mode 100644 index 0000000..86f7d63 --- /dev/null +++ b/interfaces/08-method-embedding/money.go @@ -0,0 +1,16 @@ +// 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 + +func (m money) string() string { + return fmt.Sprintf("$%.2f", m) +} diff --git a/interfaces/08-method-embedding/product.go b/interfaces/08-method-embedding/product.go new file mode 100644 index 0000000..aabd77c --- /dev/null +++ b/interfaces/08-method-embedding/product.go @@ -0,0 +1,23 @@ +// 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 +} + +func (p *product) print() { + fmt.Printf("%-15s: %s\n", p.title, p.price.string()) +} + +func (p *product) discount(ratio float64) { + p.price *= money(1 - ratio) +} diff --git a/interfaces/08-method-embedding/puzzle.go b/interfaces/08-method-embedding/puzzle.go new file mode 100644 index 0000000..acbaa47 --- /dev/null +++ b/interfaces/08-method-embedding/puzzle.go @@ -0,0 +1,12 @@ +// 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 { + product +} diff --git a/interfaces/08-method-embedding/toy.go b/interfaces/08-method-embedding/toy.go new file mode 100644 index 0000000..ab5613d --- /dev/null +++ b/interfaces/08-method-embedding/toy.go @@ -0,0 +1,12 @@ +// 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 { + product +}