add: interfaces assertion and composition (wip)
This commit is contained in:
@ -22,10 +22,6 @@ func main() {
|
||||
// printer(tetris).discount(.8)
|
||||
store.print()
|
||||
|
||||
// type printer interface {
|
||||
// print()
|
||||
// }
|
||||
|
||||
// p can store a value of any type that has a `print()` method
|
||||
var p printer
|
||||
p = puzzle{title: "sidewinder", price: 10}
|
||||
|
20
interfaces/04-assertion/book.go
Normal file
20
interfaces/04-assertion/book.go
Normal 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)
|
||||
}
|
24
interfaces/04-assertion/game.go
Normal file
24
interfaces/04-assertion/game.go
Normal 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)
|
||||
}
|
84
interfaces/04-assertion/list.go
Normal file
84
interfaces/04-assertion/list.go
Normal 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()
|
||||
// }
|
||||
// }
|
28
interfaces/04-assertion/main.go
Normal file
28
interfaces/04-assertion/main.go
Normal 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()
|
23
interfaces/04-assertion/puzzle.go
Normal file
23
interfaces/04-assertion/puzzle.go
Normal 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)
|
||||
}
|
25
interfaces/05-composition/book.go
Normal file
25
interfaces/05-composition/book.go
Normal 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 float64
|
||||
readTime int
|
||||
}
|
||||
|
||||
func (b book) print() {
|
||||
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
|
||||
}
|
||||
|
||||
// TODO: NEW
|
||||
func (b book) sum() float64 {
|
||||
return b.price
|
||||
}
|
17
interfaces/05-composition/discount.go
Normal file
17
interfaces/05-composition/discount.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
33
interfaces/05-composition/game.go
Normal file
33
interfaces/05-composition/game.go
Normal 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 float64
|
||||
playTime int
|
||||
}
|
||||
|
||||
func (g *game) print() {
|
||||
fmt.Printf("%-15s: $%.2f\n", g.title, g.price)
|
||||
}
|
||||
|
||||
// func (g *game) String() string {
|
||||
// return fmt.Sprintf("%-15s: $%.2f", g.title, g.price)
|
||||
// }
|
||||
|
||||
func (g *game) discount(ratio float64) {
|
||||
g.price *= (1 - ratio)
|
||||
}
|
||||
|
||||
// TODO: NEW
|
||||
func (g *game) sum() float64 {
|
||||
return g.price
|
||||
}
|
34
interfaces/05-composition/list.go
Normal file
34
interfaces/05-composition/list.go
Normal 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 float64) {
|
||||
for _, it := range l {
|
||||
n += it.sum()
|
||||
}
|
||||
return n
|
||||
}
|
43
interfaces/05-composition/main.go
Normal file
43
interfaces/05-composition/main.go
Normal 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)
|
||||
}
|
24
interfaces/05-composition/puzzle.go
Normal file
24
interfaces/05-composition/puzzle.go
Normal 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 float64
|
||||
}
|
||||
|
||||
func (p puzzle) print() {
|
||||
fmt.Printf("%-15s: $%.2f \n", p.title, p.price)
|
||||
}
|
||||
|
||||
// TODO: NEW
|
||||
func (p puzzle) sum() float64 {
|
||||
return p.price
|
||||
}
|
19
interfaces/05-composition/time.go
Normal file
19
interfaces/05-composition/time.go
Normal 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
|
||||
}
|
23
interfaces/05-composition/types.go
Normal file
23
interfaces/05-composition/types.go
Normal 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() float64
|
||||
}
|
||||
|
||||
// TODO: NEW
|
||||
// interface embedding
|
||||
// When an interface includes multiple methods,
|
||||
// choose a name that accurately describes its purpose.
|
||||
type item interface {
|
||||
printer
|
||||
summer
|
||||
}
|
Reference in New Issue
Block a user