rename: interfaces type assertion

This commit is contained in:
Inanc Gumus
2019-09-27 11:15:17 +03:00
parent c4fec660fa
commit 7d22851259
7 changed files with 0 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
// 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
}
func (b book) print() {
fmt.Printf("%-15s: %s\n", b.title, b.price.string())
}
+23
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 game struct {
title string
price money
}
func (g *game) print() {
fmt.Printf("%-15s: %s\n", g.title, g.price.string())
}
func (g *game) discount(ratio float64) {
g.price *= money(1 - ratio)
}
+78
View File
@@ -0,0 +1,78 @@
// 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()
// use type assertion when you cannot change the interface.
// discount(ratio float64)
}
type list []printer
func (l list) print() {
if len(l) == 0 {
fmt.Println("Sorry. We're waiting for delivery 🚚.")
return
}
for _, it := range l {
it.print()
}
}
// type assertion can extract the wrapped value.
// or: it can put the value into another interface.
func (l list) discount(ratio float64) {
// you can declare an interface in a function/method as well.
// interface is just a type.
type discounter interface {
discount(float64)
}
for _, it := range l {
// you can assert to an interface.
// and extract another interface.
if it, ok := it.(discounter); ok {
it.discount(ratio)
}
}
}
// ----
// func (l list) discount(ratio float64) {
// for _, it := range l {
// // you can inline-assert interfaces
// // interface is just a type.
// g, ok := it.(interface{ discount(float64) })
// if !ok {
// continue
// }
// g.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 {
// g, ok := it.(*game)
// if !ok {
// continue
// }
// g.discount(ratio)
// }
// }
+31
View File
@@ -0,0 +1,31 @@
// 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}
yoda = toy{title: "yoda", price: 150}
)
var store list
store = append(store, &minecraft, &tetris, mobydick, rubik, &yoda)
// #2
store.discount(.5)
store.print()
// #1
// var p printer
// p = &tetris
// tetris.discount(.5)
// p.print()
}
+16
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)
}
+19
View File
@@ -0,0 +1,19 @@
// 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())
}
+23
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 toy struct {
title string
price money
}
func (t *toy) print() {
fmt.Printf("%-15s: %s\n", t.title, t.price.string())
}
func (t *toy) discount(ratio float64) {
t.price *= money(1 - ratio)
}