From ce5a32ecbeb6ed844681d9cddab9f49dca6f3aee Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Wed, 21 Aug 2019 23:53:47 +0300 Subject: [PATCH] add: money type to interfaces --- interfaces/01-methods/book.go | 6 +++--- interfaces/02-receivers/book.go | 2 +- interfaces/03-nonstructs/book.go | 5 ++--- interfaces/03-nonstructs/game.go | 17 +++-------------- interfaces/03-nonstructs/list.go | 4 ++-- interfaces/03-nonstructs/main.go | 13 +++++++------ interfaces/03-nonstructs/money.go | 17 +++++++++++++++++ interfaces/04-interfaces/book.go | 4 ++-- interfaces/04-interfaces/game.go | 8 ++++---- interfaces/04-interfaces/money.go | 16 ++++++++++++++++ interfaces/04-interfaces/puzzle.go | 4 ++-- interfaces/05-assertion/book.go | 6 +++--- interfaces/05-assertion/game.go | 8 ++++---- interfaces/05-assertion/money.go | 16 ++++++++++++++++ interfaces/05-assertion/puzzle.go | 6 +++--- interfaces/06-composition/book.go | 6 +++--- interfaces/06-composition/game.go | 8 ++++---- interfaces/06-composition/list.go | 2 +- interfaces/06-composition/money.go | 16 ++++++++++++++++ interfaces/06-composition/puzzle.go | 6 +++--- interfaces/06-composition/types.go | 2 +- 21 files changed, 113 insertions(+), 59 deletions(-) create mode 100644 interfaces/03-nonstructs/money.go create mode 100644 interfaces/04-interfaces/money.go create mode 100644 interfaces/05-assertion/money.go create mode 100644 interfaces/06-composition/money.go diff --git a/interfaces/01-methods/book.go b/interfaces/01-methods/book.go index a001e71..22c0940 100644 --- a/interfaces/01-methods/book.go +++ b/interfaces/01-methods/book.go @@ -16,7 +16,7 @@ type book struct { func (b book) print() { // b is a copy of the original `book` value here. - fmt.Printf("%-15s: $%.2f \n", b.title, b.price) + fmt.Printf("%-15s: $%.2f\n", b.title, b.price) } // ---------------------------------------------------------------------------- @@ -25,12 +25,12 @@ func (b book) print() { // // func (b book) printBook() { // // b is a copy of the original `book` value here. -// fmt.Printf("%-15s: $%.2f \n", b.title, b.price) +// fmt.Printf("%-15s: $%.2f\n", b.title, b.price) // } // ---------------------------------------------------------------------------- // b is a copy of the original `book` value here. // // func printBook(b book) { -// fmt.Printf("%-15s: $%.2f \n", b.title, b.price) +// fmt.Printf("%-15s: $%.2f\n", b.title, b.price) // } diff --git a/interfaces/02-receivers/book.go b/interfaces/02-receivers/book.go index 2d01091..c634e56 100644 --- a/interfaces/02-receivers/book.go +++ b/interfaces/02-receivers/book.go @@ -15,5 +15,5 @@ type book struct { } func (b book) print() { - fmt.Printf("%-15s: $%.2f \n", b.title, b.price) + fmt.Printf("%-15s: $%.2f\n", b.title, b.price) } diff --git a/interfaces/03-nonstructs/book.go b/interfaces/03-nonstructs/book.go index 7b925aa..9b0ebaa 100644 --- a/interfaces/03-nonstructs/book.go +++ b/interfaces/03-nonstructs/book.go @@ -11,10 +11,9 @@ import "fmt" type book struct { title string - price float64 + price money } func (b book) print() { - // b is a copy of the original `book` value here. - fmt.Printf("%-15s: $%.2f \n", b.title, b.price) + fmt.Printf("%-15s: %s\n", b.title, b.price.string()) } diff --git a/interfaces/03-nonstructs/game.go b/interfaces/03-nonstructs/game.go index dcaf1cf..b51bf39 100644 --- a/interfaces/03-nonstructs/game.go +++ b/interfaces/03-nonstructs/game.go @@ -11,24 +11,13 @@ import "fmt" type game struct { title string - price float64 + price money } func (g *game) print() { - fmt.Printf("%-15s: $%.2f\n", g.title, g.price) + fmt.Printf("%-15s: %s\n", g.title, g.price.string()) } -// + discount gets a copy of `*game`. -// + discount can update the original `game` through the game pointer. -// + it's better to use the same receiver type: `*game` for all methods. func (g *game) discount(ratio float64) { - g.price *= (1 - ratio) + g.price *= money(1 - ratio) } - -// PREVIOUS CODE: - -// ---------------------------------------------------------------------------- -// + `g` is a copy: `discount` cannot change the original `g`. -// func (g game) discount(ratio float64) { -// g.price *= (1 - ratio) -// } diff --git a/interfaces/03-nonstructs/list.go b/interfaces/03-nonstructs/list.go index 655fc2a..a184b52 100644 --- a/interfaces/03-nonstructs/list.go +++ b/interfaces/03-nonstructs/list.go @@ -9,9 +9,9 @@ package main import "fmt" -// + you can attach methods to any concrete type. +// + you can attach methods to non-struct types. // + rule: you need to declare a new type in the same package. -type list []game +type list []*game func (l list) print() { // `list` acts like a `[]game` diff --git a/interfaces/03-nonstructs/main.go b/interfaces/03-nonstructs/main.go index deba14e..748f39e 100644 --- a/interfaces/03-nonstructs/main.go +++ b/interfaces/03-nonstructs/main.go @@ -14,14 +14,15 @@ func main() { tetris = game{title: "tetris", price: 5} ) - var items []game - items = append(items, minecraft, tetris) + var items []*game + items = append(items, &minecraft, &tetris) // you can attach methods to a compatible type on the fly: - my := list([]game{minecraft, tetris}) - - // you can call methods even on a nil value + // items -> []*game + // list -> []*game + my := list(items) // my = nil - my.print() // or: list.print(items) + // you can call methods even on a nil value + my.print() } diff --git a/interfaces/03-nonstructs/money.go b/interfaces/03-nonstructs/money.go new file mode 100644 index 0000000..a705ac6 --- /dev/null +++ b/interfaces/03-nonstructs/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 + +func (m money) string() string { + // $xx.yy + return fmt.Sprintf("$%.2f", m) +} diff --git a/interfaces/04-interfaces/book.go b/interfaces/04-interfaces/book.go index 2d01091..9b0ebaa 100644 --- a/interfaces/04-interfaces/book.go +++ b/interfaces/04-interfaces/book.go @@ -11,9 +11,9 @@ import "fmt" type book struct { title string - price float64 + price money } func (b book) print() { - fmt.Printf("%-15s: $%.2f \n", b.title, b.price) + fmt.Printf("%-15s: %s\n", b.title, b.price.string()) } diff --git a/interfaces/04-interfaces/game.go b/interfaces/04-interfaces/game.go index e8fafc6..b51bf39 100644 --- a/interfaces/04-interfaces/game.go +++ b/interfaces/04-interfaces/game.go @@ -11,13 +11,13 @@ import "fmt" type game struct { title string - price float64 + price money } func (g *game) print() { - fmt.Printf("%-15s: $%.2f\n", g.title, g.price) + fmt.Printf("%-15s: %s\n", g.title, g.price.string()) } func (g *game) discount(ratio float64) { - g.price *= (1 - ratio) -} \ No newline at end of file + g.price *= money(1 - ratio) +} diff --git a/interfaces/04-interfaces/money.go b/interfaces/04-interfaces/money.go new file mode 100644 index 0000000..86f7d63 --- /dev/null +++ b/interfaces/04-interfaces/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/04-interfaces/puzzle.go b/interfaces/04-interfaces/puzzle.go index 09f5156..5ea12b9 100644 --- a/interfaces/04-interfaces/puzzle.go +++ b/interfaces/04-interfaces/puzzle.go @@ -11,9 +11,9 @@ import "fmt" type puzzle struct { title string - price float64 + price money } func (p puzzle) print() { - fmt.Printf("%-15s: $%.2f \n", p.title, p.price) + fmt.Printf("%-15s: %s\n", p.title, p.price.string()) } diff --git a/interfaces/05-assertion/book.go b/interfaces/05-assertion/book.go index c7c114b..a55520f 100644 --- a/interfaces/05-assertion/book.go +++ b/interfaces/05-assertion/book.go @@ -11,10 +11,10 @@ import "fmt" type book struct { title string - price float64 + price money readTime int } func (b book) print() { - fmt.Printf("%-15s: $%.2f \n", b.title, b.price) -} \ No newline at end of file + fmt.Printf("%-15s: %s\n", b.title, b.price.string()) +} diff --git a/interfaces/05-assertion/game.go b/interfaces/05-assertion/game.go index 7503066..2705bee 100644 --- a/interfaces/05-assertion/game.go +++ b/interfaces/05-assertion/game.go @@ -11,14 +11,14 @@ import "fmt" type game struct { title string - price float64 + price money playTime int } func (g *game) print() { - fmt.Printf("%-15s: $%.2f\n", g.title, g.price) + fmt.Printf("%-15s: %s\n", g.title, g.price.string()) } func (g *game) discount(ratio float64) { - g.price *= (1 - ratio) -} \ No newline at end of file + g.price *= money(1 - ratio) +} diff --git a/interfaces/05-assertion/money.go b/interfaces/05-assertion/money.go new file mode 100644 index 0000000..86f7d63 --- /dev/null +++ b/interfaces/05-assertion/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/05-assertion/puzzle.go b/interfaces/05-assertion/puzzle.go index 6776092..de54585 100644 --- a/interfaces/05-assertion/puzzle.go +++ b/interfaces/05-assertion/puzzle.go @@ -11,13 +11,13 @@ import "fmt" type puzzle struct { title string - price float64 + price money } func (p *puzzle) print() { - fmt.Printf("%-15s: $%.2f \n", p.title, p.price) + fmt.Printf("%-15s: %s\n", p.title, p.price.string()) } func (p *puzzle) discount(ratio float64) { - p.price *= (1 - ratio) + p.price *= money(1 - ratio) } diff --git a/interfaces/06-composition/book.go b/interfaces/06-composition/book.go index 9fec6eb..4d1e5ac 100644 --- a/interfaces/06-composition/book.go +++ b/interfaces/06-composition/book.go @@ -11,15 +11,15 @@ import "fmt" type book struct { title string - price float64 + price money readTime int } func (b book) print() { - fmt.Printf("%-15s: $%.2f \n", b.title, b.price) + fmt.Printf("%-15s: %s\n", b.title, b.price.string()) } // TODO: NEW -func (b book) sum() float64 { +func (b book) sum() money { return b.price } diff --git a/interfaces/06-composition/game.go b/interfaces/06-composition/game.go index 0ff457e..47ba4e6 100644 --- a/interfaces/06-composition/game.go +++ b/interfaces/06-composition/game.go @@ -11,12 +11,12 @@ import "fmt" type game struct { title string - price float64 + price money playTime int } func (g *game) print() { - fmt.Printf("%-15s: $%.2f\n", g.title, g.price) + fmt.Printf("%-15s: %s\n", g.title, g.price.string()) } // func (g *game) String() string { @@ -24,10 +24,10 @@ func (g *game) print() { // } func (g *game) discount(ratio float64) { - g.price *= (1 - ratio) + g.price *= money(1 - ratio) } // TODO: NEW -func (g *game) sum() float64 { +func (g *game) sum() money { return g.price } diff --git a/interfaces/06-composition/list.go b/interfaces/06-composition/list.go index cd34e48..4bbf89d 100644 --- a/interfaces/06-composition/list.go +++ b/interfaces/06-composition/list.go @@ -26,7 +26,7 @@ func (l list) print() { } // TODO: NEW -func (l list) sum() (n float64) { +func (l list) sum() (n money) { for _, it := range l { n += it.sum() } diff --git a/interfaces/06-composition/money.go b/interfaces/06-composition/money.go new file mode 100644 index 0000000..86f7d63 --- /dev/null +++ b/interfaces/06-composition/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/06-composition/puzzle.go b/interfaces/06-composition/puzzle.go index cd3e133..52dd4c1 100644 --- a/interfaces/06-composition/puzzle.go +++ b/interfaces/06-composition/puzzle.go @@ -11,14 +11,14 @@ import "fmt" type puzzle struct { title string - price float64 + price money } func (p puzzle) print() { - fmt.Printf("%-15s: $%.2f \n", p.title, p.price) + fmt.Printf("%-15s: %s\n", p.title, p.price.string()) } // TODO: NEW -func (p puzzle) sum() float64 { +func (p puzzle) sum() money { return p.price } diff --git a/interfaces/06-composition/types.go b/interfaces/06-composition/types.go index 6ea4882..8a10e3a 100644 --- a/interfaces/06-composition/types.go +++ b/interfaces/06-composition/types.go @@ -10,7 +10,7 @@ type printer interface { // TODO: NEW type summer interface { - sum() float64 + sum() money } // TODO: NEW