add: method embedding
This commit is contained in:
60
interfaces/08-method-embedding/book.go
Normal file
60
interfaces/08-method-embedding/book.go
Normal file
@ -0,0 +1,60 @@
|
||||
// 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"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type book struct {
|
||||
// embed the product type into the book type.
|
||||
// all the fields and methods of the product will be
|
||||
// available in this book type.
|
||||
product
|
||||
published interface{}
|
||||
}
|
||||
|
||||
// book type's print method takes priority.
|
||||
//
|
||||
// + when you call it on a book value, the following method will
|
||||
// be executed.
|
||||
//
|
||||
// + if it wasn't here, the product type's print method would
|
||||
// have been executed.
|
||||
func (b *book) print() {
|
||||
// the book can also call the embedded product's print method
|
||||
// if it wants to, as in here:
|
||||
b.product.print()
|
||||
|
||||
p := format(b.published)
|
||||
fmt.Printf("\t - (%v)\n", p)
|
||||
}
|
||||
|
||||
func format(v interface{}) string {
|
||||
var t int
|
||||
|
||||
switch v := v.(type) {
|
||||
case int:
|
||||
// book{title: "moby dick", price: 10, published: 118281600},
|
||||
t = v
|
||||
case string:
|
||||
// book{title: "odyssey", price: 15, published: "733622400"},
|
||||
t, _ = strconv.Atoi(v)
|
||||
default:
|
||||
// book{title: "hobbit", price: 25},
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// Mon Jan 2 15:04:05 -0700 MST 2006
|
||||
const layout = "2006/01"
|
||||
|
||||
u := time.Unix(int64(t), 0)
|
||||
return u.Format(layout)
|
||||
}
|
12
interfaces/08-method-embedding/game.go
Normal file
12
interfaces/08-method-embedding/game.go
Normal file
@ -0,0 +1,12 @@
|
||||
// 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
|
||||
|
||||
type game struct {
|
||||
product
|
||||
}
|
39
interfaces/08-method-embedding/list.go
Normal file
39
interfaces/08-method-embedding/list.go
Normal file
@ -0,0 +1,39 @@
|
||||
// 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. We're waiting for 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)
|
||||
}
|
||||
}
|
||||
}
|
29
interfaces/08-method-embedding/main.go
Normal file
29
interfaces/08-method-embedding/main.go
Normal file
@ -0,0 +1,29 @@
|
||||
// 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() {
|
||||
store := list{
|
||||
&book{product{"moby dick", 10}, 118281600},
|
||||
&book{product{"odyssey", 15}, "733622400"},
|
||||
&book{product{"hobbit", 25}, nil},
|
||||
&puzzle{product{"rubik's cube", 5}},
|
||||
&game{product{"minecraft", 20}},
|
||||
&game{product{"tetris", 5}},
|
||||
&toy{product{"yoda", 150}},
|
||||
}
|
||||
|
||||
store.discount(.5)
|
||||
store.print()
|
||||
|
||||
// t := &toy{product{"yoda", 150}}
|
||||
// fmt.Printf("%#v\n", t)
|
||||
|
||||
// b := &book{product{"moby dick", 10}, 118281600}
|
||||
// fmt.Printf("%#v\n", b)
|
||||
}
|
16
interfaces/08-method-embedding/money.go
Normal file
16
interfaces/08-method-embedding/money.go
Normal 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)
|
||||
}
|
23
interfaces/08-method-embedding/product.go
Normal file
23
interfaces/08-method-embedding/product.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 product struct {
|
||||
title string
|
||||
price money
|
||||
}
|
||||
|
||||
func (p *product) print() {
|
||||
fmt.Printf("%-15s: %s\n", p.title, p.price.string())
|
||||
}
|
||||
|
||||
func (p *product) discount(ratio float64) {
|
||||
p.price *= money(1 - ratio)
|
||||
}
|
12
interfaces/08-method-embedding/puzzle.go
Normal file
12
interfaces/08-method-embedding/puzzle.go
Normal file
@ -0,0 +1,12 @@
|
||||
// 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
|
||||
|
||||
type puzzle struct {
|
||||
product
|
||||
}
|
12
interfaces/08-method-embedding/toy.go
Normal file
12
interfaces/08-method-embedding/toy.go
Normal file
@ -0,0 +1,12 @@
|
||||
// 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
|
||||
|
||||
type toy struct {
|
||||
product
|
||||
}
|
Reference in New Issue
Block a user