add: type switch
This commit is contained in:
17
interfaces/08-composition/discount.go
Normal file
17
interfaces/08-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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user