update: little fixes to composition

This commit is contained in:
Inanc Gumus
2019-09-02 19:07:24 +03:00
parent 86a59253d1
commit e1fb0a4a74
2 changed files with 23 additions and 23 deletions

View File

@ -29,7 +29,7 @@ func (l list) String() string {
for _, it := range l { for _, it := range l {
fmt.Fprintf(&str, "%s\n", it) fmt.Fprintf(&str, "%s\n", it)
} }
fmt.Fprintf(&str, "\tTOTAL : $%.2f", l.sum()) fmt.Fprintf(&str, "\tTOTAL : $%.2f\n", l.sum())
return str.String() return str.String()
} }

View File

@ -10,7 +10,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"sort"
) )
func main() { func main() {
@ -24,34 +23,35 @@ func main() {
&toy{product{"yoda", 150}}, &toy{product{"yoda", 150}},
} }
out, _ := json.Marshal(store) out, _ := json.MarshalIndent(store, "", "\t")
fmt.Println(string(out)) fmt.Println(string(out))
var ( // store.discount(.5)
nilItem item // fmt.Print(store)
nilBook *book
)
fmt.Println("nilBook ?", nilBook == nil) // var (
fmt.Println("nilItem?", nilItem == nil) // nilItem item
// nilBook *book
// )
nilItem = nilBook // fmt.Println("nilBook ?", nilBook == nil)
fmt.Println("nilItem?", nilItem == nil) // fmt.Println("nilItem?", nilItem == nil)
books := store[:3] // nilItem = nilBook
// fmt.Println("nilItem?", nilItem == nil)
books.discount(.5) // books := store[:3]
fmt.Printf("%s\n", store)
others := store[3:] // books.discount(.5)
sort.Sort(byPrice(others)) // fmt.Printf("%s\n", store)
fmt.Printf("\n%s\n", store)
store = list{books, others} // others := store[3:]
fmt.Printf("\n%s\n", store) // sort.Sort(byPrice(others))
// fmt.Printf("\n%s\n", store)
store.discount(.5) // store = list{books, others}
// sort.Sort(sort.Reverse(byPrice(store))) // fmt.Printf("\n%s\n", store)
sort.Sort(byName(store))
fmt.Println(store) // // sort.Sort(sort.Reverse(byPrice(store)))
// sort.Sort(byName(store))
} }