From aa7fada2aaccce59d68bb44cbfab169501f7a60c Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Mon, 19 Aug 2019 20:01:02 +0300 Subject: [PATCH] update: interfaces --- interfaces/02-receivers/list.go | 2 - interfaces/03-interfaces/book.go | 1 - interfaces/03-interfaces/game.go | 13 +----- interfaces/03-interfaces/list.go | 2 - interfaces/03-interfaces/main.go | 35 ++++------------ interfaces/04-interfaces/book.go | 24 ----------- interfaces/04-interfaces/game.go | 28 ------------- interfaces/04-interfaces/list.go | 71 -------------------------------- interfaces/04-interfaces/main.go | 35 ---------------- 9 files changed, 9 insertions(+), 202 deletions(-) delete mode 100644 interfaces/04-interfaces/book.go delete mode 100644 interfaces/04-interfaces/game.go delete mode 100644 interfaces/04-interfaces/list.go delete mode 100644 interfaces/04-interfaces/main.go diff --git a/interfaces/02-receivers/list.go b/interfaces/02-receivers/list.go index da097a3..655fc2a 100644 --- a/interfaces/02-receivers/list.go +++ b/interfaces/02-receivers/list.go @@ -20,8 +20,6 @@ func (l list) print() { return } - fmt.Printf("My Store:\n") - fmt.Printf("---------\n") for _, it := range l { it.print() } diff --git a/interfaces/03-interfaces/book.go b/interfaces/03-interfaces/book.go index 7b925aa..2d01091 100644 --- a/interfaces/03-interfaces/book.go +++ b/interfaces/03-interfaces/book.go @@ -15,6 +15,5 @@ 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) } diff --git a/interfaces/03-interfaces/game.go b/interfaces/03-interfaces/game.go index dcaf1cf..e8fafc6 100644 --- a/interfaces/03-interfaces/game.go +++ b/interfaces/03-interfaces/game.go @@ -18,17 +18,6 @@ func (g *game) print() { fmt.Printf("%-15s: $%.2f\n", g.title, g.price) } -// + 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) -} - -// PREVIOUS CODE: - -// ---------------------------------------------------------------------------- -// + `g` is a copy: `discount` cannot change the original `g`. -// func (g game) discount(ratio float64) { -// g.price *= (1 - ratio) -// } +} \ No newline at end of file diff --git a/interfaces/03-interfaces/list.go b/interfaces/03-interfaces/list.go index 1c9c2e9..7416ced 100644 --- a/interfaces/03-interfaces/list.go +++ b/interfaces/03-interfaces/list.go @@ -21,8 +21,6 @@ func (l list) print() { return } - fmt.Printf("My Store:\n") - fmt.Printf("---------\n") for _, it := range l { it.print() diff --git a/interfaces/03-interfaces/main.go b/interfaces/03-interfaces/main.go index 0e80a9e..2ba16b7 100644 --- a/interfaces/03-interfaces/main.go +++ b/interfaces/03-interfaces/main.go @@ -16,37 +16,18 @@ func main() { ) var store list - store = append(store, &minecraft, &tetris, mobydick, rubik) tetris.discount(.8) // printer(tetris).discount(.8) - store.print() -} -/* -var my list + // type printer interface { + // print() + // } -// + only the `*game` have methods. -// + `game` doesn't have any methods. -// + `item` interface couldn't be satisfied with a `game` value. -// + it can only be satisfied with a `*game` value. -// my = my.add(minecraft) -my = append(my, &minecraft) -my = append(my, &tetris) -my = append(my, &mobydick) - -// interface value stores a pointer to minecraft -minecraft.discount(.5) -my.list() - -my.discount(.5) -my.list() -*/ - -// ! cannot add mobydick to the store: it's a book type, not a game type. -// interfaces to the rescue! -// mobydick := book{title: "moby dick", price: 10} -// my.add(&mobydick) -// ---------------------------------------------------------------- + // p can store a value of any type that has a `print()` method + var p printer + p = puzzle{title: "sidewinder", price: 10} + p.print() +} \ No newline at end of file diff --git a/interfaces/04-interfaces/book.go b/interfaces/04-interfaces/book.go deleted file mode 100644 index 4cfccba..0000000 --- a/interfaces/04-interfaces/book.go +++ /dev/null @@ -1,24 +0,0 @@ -// 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 book struct { - title string - price float64 -} - -func (b book) print() { - fmt.Printf("%-15s: $%.2f \n", b.title, b.price) -} - -// TODO: NEW -func (b book) total() float64 { - return b.price -} diff --git a/interfaces/04-interfaces/game.go b/interfaces/04-interfaces/game.go deleted file mode 100644 index 7cbe528..0000000 --- a/interfaces/04-interfaces/game.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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 game struct { - title string - price float64 -} - -func (g *game) print() { - fmt.Printf("%-15s: $%.2f\n", g.title, g.price) -} - -func (g *game) discount(ratio float64) { - g.price *= (1 - ratio) -} - -// TODO: NEW -func (g *game) total() float64 { - return g.price -} diff --git a/interfaces/04-interfaces/list.go b/interfaces/04-interfaces/list.go deleted file mode 100644 index 86a0156..0000000 --- a/interfaces/04-interfaces/list.go +++ /dev/null @@ -1,71 +0,0 @@ -// 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" - -// TODO: NEW -type printer interface { - print() -} - -// TODO: NEW -type pricer interface { - total() float64 -} - -// TODO: NEW -// interface embedding -type item interface { - printer - pricer -} - -// a slice of item interface values -type list []item - -func (l list) list() { - // `list` acts like a `[]game` - if len(l) == 0 { - fmt.Println("Sorry. Our store is closed. We're waiting for the delivery 🚚.") - return - } - - fmt.Printf("My Store:\n") - fmt.Printf("---------\n") - for _, it := range l { - it.print() - } - - // TODO: NEW - fmt.Printf("\nGROSS: $%.2f\n", l.total()) -} - -// TODO: NEW -func (l list) total() (gross float64) { - for _, it := range l { - gross += it.total() - } - return gross -} - -// TODO: NEW -func (l list) discount(ratio float64) { - type discounter interface { - discount(float64) - } - - for _, it := range l { - dit, ok := it.(discounter) - if !ok { - continue - } - - dit.discount(ratio) - } -} diff --git a/interfaces/04-interfaces/main.go b/interfaces/04-interfaces/main.go deleted file mode 100644 index b3ac142..0000000 --- a/interfaces/04-interfaces/main.go +++ /dev/null @@ -1,35 +0,0 @@ -// 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() { - var ( - minecraft = game{title: "minecraft", price: 20} - tetris = game{title: "tetris", price: 5} - mobydick = book{title: "moby dick", price: 10} - ) - - var my list - // my.list() // nil receiver is a valid value - - // + only the `*game` have methods. - // + `game` doesn't have any methods. - // + `item` interface couldn't be satisfied with a `game` value. - // + it can only be satisfied with a `*game` value. - // my = my.add(minecraft) - my = append(my, &minecraft) - my = append(my, &tetris) - my = append(my, &mobydick) - - // interface value stores a pointer to minecraft - minecraft.discount(.5) - my.list() - - my.discount(.5) - my.list() -}