add: method embedding

This commit is contained in:
Inanc Gumus
2019-08-31 20:28:08 +03:00
parent 9f8bc6ea89
commit 9bcaae2505
8 changed files with 203 additions and 0 deletions

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