add: type switch

This commit is contained in:
Inanc Gumus
2019-08-25 08:57:53 +03:00
parent 39aed37a88
commit 0f07e4814e
27 changed files with 355 additions and 130 deletions

View File

@@ -0,0 +1,25 @@
// 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 money
readTime int
}
func (b book) print() {
fmt.Printf("%-15s: %s\n", b.title, b.price.string())
}
// TODO: NEW
func (b book) sum() money {
return b.price
}

View File

@@ -0,0 +1,17 @@
package main
// usually: include the discount method in the list.go
// it is here for clarity
// TODO: NEW
func (l list) discount(ratio float64) {
type discounter interface {
discount(float64)
}
for _, it := range l {
if it, ok := it.(discounter); ok {
it.discount(ratio)
}
}
}

View File

@@ -0,0 +1,33 @@
// 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 money
playTime int
}
func (g *game) print() {
fmt.Printf("%-15s: %s\n", g.title, g.price.string())
}
// func (g *game) String() string {
// return fmt.Sprintf("%-15s: $%.2f", g.title, g.price)
// }
func (g *game) discount(ratio float64) {
g.price *= money(1 - ratio)
}
// TODO: NEW
func (g *game) sum() money {
return g.price
}

View File

@@ -0,0 +1,34 @@
// 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 list []item
func (l list) print() {
if len(l) == 0 {
fmt.Println("Sorry. Our store is closed. We're waiting for the delivery 🚚.")
return
}
for _, it := range l {
it.print()
}
// TODO: NEW
fmt.Printf("\tTOTAL : $%.2f\n", l.sum())
}
// TODO: NEW
func (l list) sum() (n money) {
for _, it := range l {
n += it.sum()
}
return n
}

View File

@@ -0,0 +1,43 @@
// 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() {
var (
mobydick = book{title: "moby dick", price: 10, readTime: 10}
minecraft = game{title: "minecraft", price: 20, playTime: 5}
tetris = game{title: "tetris", price: 5, playTime: 2}
rubik = puzzle{title: "rubik's cube", price: 5}
)
var store list
store = append(store, &minecraft, &tetris, mobydick, rubik)
// tetris.discount(.8)
// store.print()
store.discount(.5)
store.print()
// t := store.time()
// fmt.Printf("Total entertainment time: %d hours\n", t)
// games := store[:2]
// other := store[2:]
// games.print()
// other.print()
// list{games, other}.print()
// var b *book
// var np printer
// np = b
// // p := printer(puzzle{title: "sidewinder", price: 10})
// fmt.Println(np == nil)
}

View File

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

View File

@@ -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 puzzle struct {
title string
price money
}
func (p puzzle) print() {
fmt.Printf("%-15s: %s\n", p.title, p.price.string())
}
// TODO: NEW
func (p puzzle) sum() money {
return p.price
}

View File

@@ -0,0 +1,19 @@
package main
// usually: include the time method in the list.go
// it is here for clarity
// TODO: NEW
// you could include a time method in the book and game instead.
// but sometimes it is not possible to do so.
func (l list) time() (total int) {
for _, it := range l {
switch it := it.(type) {
case *game:
total += it.playTime
case book:
total += it.readTime
}
}
return total
}

View File

@@ -0,0 +1,23 @@
package main
// don't separate your interfaces like this.
// put them in the same file where you need to use them.
// this is here for clarity
type printer interface {
print()
}
// TODO: NEW
type summer interface {
sum() money
}
// TODO: NEW
// interface embedding
// When an interface includes multiple methods,
// choose a name that accurately describes its purpose.
type item interface {
printer
summer
}