update: interfaces composition
This commit is contained in:
@ -8,19 +8,29 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type book struct {
|
type book struct {
|
||||||
*product
|
product
|
||||||
published interface{}
|
Published interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *book) String() string {
|
func (b *book) String() string {
|
||||||
p := format(b.published)
|
p := format(b.Published)
|
||||||
return fmt.Sprintf("%s - (%v)", b.product, p)
|
return fmt.Sprintf("%s - (%v)", &b.product, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *book) MarshalJSON() ([]byte, error) {
|
||||||
|
type jbook book
|
||||||
|
|
||||||
|
jb := (*jbook)(b)
|
||||||
|
jb.Published = format(b.Published)
|
||||||
|
|
||||||
|
return json.Marshal(jb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func format(v interface{}) string {
|
func format(v interface{}) string {
|
||||||
|
@ -8,5 +8,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
type game struct {
|
type game struct {
|
||||||
*product
|
product
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type summer interface {
|
|
||||||
sum() money
|
|
||||||
}
|
|
||||||
|
|
||||||
type item interface {
|
type item interface {
|
||||||
summer // same as: `sum() money`
|
sum() money
|
||||||
|
discount(float64)
|
||||||
fmt.Stringer // same as: `String() string`
|
fmt.Stringer // same as: `String() string`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,13 +42,7 @@ func (l list) sum() (total money) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l list) discount(ratio float64) {
|
func (l list) discount(ratio float64) {
|
||||||
type discounter interface {
|
|
||||||
discount(float64)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, it := range l {
|
for _, it := range l {
|
||||||
if it, ok := it.(discounter); ok {
|
it.discount(ratio)
|
||||||
it.discount(ratio)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,35 +8,50 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
store := list{
|
store := list{
|
||||||
&book{&product{"moby dick", 10}, 118281600},
|
&book{product{"moby dick", 10}, 118281600},
|
||||||
&book{&product{"odyssey", 15}, "733622400"},
|
&book{product{"odyssey", 15}, "733622400"},
|
||||||
&book{&product{"hobbit", 25}, nil},
|
&book{product{"hobbit", 25}, nil},
|
||||||
&puzzle{&product{"rubik's cube", 5}},
|
&puzzle{product{"rubik's cube", 5}},
|
||||||
&game{&product{"minecraft", 20}},
|
&game{product{"minecraft", 20}},
|
||||||
&game{&product{"tetris", 5}},
|
&game{product{"tetris", 5}},
|
||||||
&toy{&product{"yoda", 150}},
|
&toy{product{"yoda", 150}},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out, _ := json.Marshal(store)
|
||||||
|
fmt.Println(string(out))
|
||||||
|
|
||||||
|
var (
|
||||||
|
nilItem item
|
||||||
|
nilBook *book
|
||||||
|
)
|
||||||
|
|
||||||
|
fmt.Println("nilBook ?", nilBook == nil)
|
||||||
|
fmt.Println("nilItem?", nilItem == nil)
|
||||||
|
|
||||||
|
nilItem = nilBook
|
||||||
|
fmt.Println("nilItem?", nilItem == nil)
|
||||||
|
|
||||||
books := store[:3]
|
books := store[:3]
|
||||||
others := store[3:]
|
|
||||||
|
|
||||||
books.discount(.5)
|
books.discount(.5)
|
||||||
fmt.Printf("%s\n", store)
|
fmt.Printf("%s\n", store)
|
||||||
|
|
||||||
|
others := store[3:]
|
||||||
sort.Sort(byPrice(others))
|
sort.Sort(byPrice(others))
|
||||||
fmt.Printf("\n%s\n", store)
|
fmt.Printf("\n%s\n", store)
|
||||||
|
|
||||||
store = list{books, others}
|
store = list{books, others}
|
||||||
fmt.Printf("\n%s\n", store)
|
fmt.Printf("\n%s\n", store)
|
||||||
|
|
||||||
// store.discount(.5)
|
store.discount(.5)
|
||||||
// // sort.Sort(sort.Reverse(byPrice(store)))
|
// sort.Sort(sort.Reverse(byPrice(store)))
|
||||||
// sort.Sort(byName(store))
|
sort.Sort(byName(store))
|
||||||
// fmt.Println(store)
|
fmt.Println(store)
|
||||||
}
|
}
|
||||||
|
@ -10,18 +10,18 @@ package main
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type product struct {
|
type product struct {
|
||||||
title string
|
Title string
|
||||||
price money
|
Price money
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *product) discount(ratio float64) {
|
func (p *product) discount(ratio float64) {
|
||||||
p.price *= money(1 - ratio)
|
p.Price *= money(1 - ratio)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *product) sum() money {
|
func (p *product) sum() money {
|
||||||
return p.price
|
return p.Price
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *product) String() string {
|
func (p *product) String() string {
|
||||||
return fmt.Sprintf("%-15s: %s", p.title, p.price)
|
return fmt.Sprintf("%-15s: %s", p.Title, p.Price)
|
||||||
}
|
}
|
||||||
|
@ -8,5 +8,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
type puzzle struct {
|
type puzzle struct {
|
||||||
*product
|
product
|
||||||
}
|
}
|
||||||
|
@ -8,5 +8,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
type toy struct {
|
type toy struct {
|
||||||
*product
|
product
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user