update: interfaces

This commit is contained in:
Inanc Gumus
2019-08-19 20:01:02 +03:00
parent b39a9ea117
commit aa7fada2aa
9 changed files with 9 additions and 202 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}
}

View File

@@ -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()
}