add: interfaces assertion and composition (wip)

This commit is contained in:
Inanc Gumus
2019-08-20 19:03:57 +03:00
parent aa7fada2aa
commit 6b95995c81
14 changed files with 397 additions and 4 deletions

View File

@@ -0,0 +1,20 @@
// 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
readTime int
}
func (b book) print() {
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
}

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 game struct {
title string
price float64
playTime int
}
func (g *game) print() {
fmt.Printf("%-15s: $%.2f\n", g.title, g.price)
}
func (g *game) discount(ratio float64) {
g.price *= (1 - ratio)
}

View File

@@ -0,0 +1,84 @@
// 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. Our store is closed. We're waiting for the 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)
}
}
}
//
// type assertion can pull out the real value behind an interface value.
//
// it can also check whether the value convertable to a given type.
//
// func (l list) discount(ratio float64) {
// for _, it := range l {
// // remember: interface is a type
// if it, ok := it.(interface{ discount(float64) }); ok {
// it.discount(ratio)
// }
// }
// }
//
// type switch can pull out the real value behind an interface value.
//
// func (l list) discount(ratio float64) {
// for _, it := range l {
// switch it := it.(type) {
// case *game:
// it.discount(ratio)
// case *puzzle:
// it.discount(ratio)
// }
// }
// }
//
// type switch can find the type behind an interface value.
//
// func (l list) discount(ratio float64) {
// for _, it := range l {
// switch it.(type) {
// case *game:
// fmt.Print("it's a *game! --> ")
// case *puzzle:
// fmt.Print("it's a *puzzle! --> ")
// default:
// fmt.Print("neither --> ")
// }
// it.print()
// }
// }

View File

@@ -0,0 +1,28 @@
// 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 (
mobydick = book{title: "moby dick", price: 10}
minecraft = game{title: "minecraft", price: 20}
tetris = game{title: "tetris", price: 5}
rubik = puzzle{title: "rubik's cube", price: 5}
)
var store list
store = append(store, &minecraft, &tetris, mobydick, &rubik)
store.discount(.5)
store.print()
// minecraft.discount(.5)
// minecraft.print()
}
// store.discount()

View File

@@ -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 puzzle struct {
title string
price float64
}
func (p *puzzle) print() {
fmt.Printf("%-15s: $%.2f \n", p.title, p.price)
}
func (p *puzzle) discount(ratio float64) {
p.price *= (1 - ratio)
}