add: money type to interfaces

This commit is contained in:
Inanc Gumus
2019-08-21 23:53:47 +03:00
parent c282da3c6d
commit ce5a32ecbe
21 changed files with 113 additions and 59 deletions

View File

@ -16,7 +16,7 @@ type book struct {
func (b book) print() {
// b is a copy of the original `book` value here.
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
fmt.Printf("%-15s: $%.2f\n", b.title, b.price)
}
// ----------------------------------------------------------------------------
@ -25,12 +25,12 @@ func (b book) print() {
//
// func (b book) printBook() {
// // b is a copy of the original `book` value here.
// fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
// fmt.Printf("%-15s: $%.2f\n", b.title, b.price)
// }
// ----------------------------------------------------------------------------
// b is a copy of the original `book` value here.
//
// func printBook(b book) {
// fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
// fmt.Printf("%-15s: $%.2f\n", b.title, b.price)
// }

View File

@ -15,5 +15,5 @@ type book struct {
}
func (b book) print() {
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
fmt.Printf("%-15s: $%.2f\n", b.title, b.price)
}

View File

@ -11,10 +11,9 @@ import "fmt"
type book struct {
title string
price float64
price money
}
func (b book) print() {
// b is a copy of the original `book` value here.
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
fmt.Printf("%-15s: %s\n", b.title, b.price.string())
}

View File

@ -11,24 +11,13 @@ import "fmt"
type game struct {
title string
price float64
price money
}
func (g *game) print() {
fmt.Printf("%-15s: $%.2f\n", g.title, g.price)
fmt.Printf("%-15s: %s\n", g.title, g.price.string())
}
// + discount gets a copy of `*game`.
// + discount can update the original `game` through the game pointer.
// + it's better to use the same receiver type: `*game` for all methods.
func (g *game) discount(ratio float64) {
g.price *= (1 - ratio)
g.price *= money(1 - ratio)
}
// PREVIOUS CODE:
// ----------------------------------------------------------------------------
// + `g` is a copy: `discount` cannot change the original `g`.
// func (g game) discount(ratio float64) {
// g.price *= (1 - ratio)
// }

View File

@ -9,9 +9,9 @@ package main
import "fmt"
// + you can attach methods to any concrete type.
// + you can attach methods to non-struct types.
// + rule: you need to declare a new type in the same package.
type list []game
type list []*game
func (l list) print() {
// `list` acts like a `[]game`

View File

@ -14,14 +14,15 @@ func main() {
tetris = game{title: "tetris", price: 5}
)
var items []game
items = append(items, minecraft, tetris)
var items []*game
items = append(items, &minecraft, &tetris)
// you can attach methods to a compatible type on the fly:
my := list([]game{minecraft, tetris})
// you can call methods even on a nil value
// items -> []*game
// list -> []*game
my := list(items)
// my = nil
my.print() // or: list.print(items)
// you can call methods even on a nil value
my.print()
}

View File

@ -0,0 +1,17 @@
// 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 {
// $xx.yy
return fmt.Sprintf("$%.2f", m)
}

View File

@ -11,9 +11,9 @@ import "fmt"
type book struct {
title string
price float64
price money
}
func (b book) print() {
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
fmt.Printf("%-15s: %s\n", b.title, b.price.string())
}

View File

@ -11,13 +11,13 @@ import "fmt"
type game struct {
title string
price float64
price money
}
func (g *game) print() {
fmt.Printf("%-15s: $%.2f\n", g.title, g.price)
fmt.Printf("%-15s: %s\n", g.title, g.price.string())
}
func (g *game) discount(ratio float64) {
g.price *= (1 - ratio)
g.price *= money(1 - ratio)
}

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

View File

@ -11,9 +11,9 @@ import "fmt"
type puzzle struct {
title string
price float64
price money
}
func (p puzzle) print() {
fmt.Printf("%-15s: $%.2f \n", p.title, p.price)
fmt.Printf("%-15s: %s\n", p.title, p.price.string())
}

View File

@ -11,10 +11,10 @@ import "fmt"
type book struct {
title string
price float64
price money
readTime int
}
func (b book) print() {
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
fmt.Printf("%-15s: %s\n", b.title, b.price.string())
}

View File

@ -11,14 +11,14 @@ import "fmt"
type game struct {
title string
price float64
price money
playTime int
}
func (g *game) print() {
fmt.Printf("%-15s: $%.2f\n", g.title, g.price)
fmt.Printf("%-15s: %s\n", g.title, g.price.string())
}
func (g *game) discount(ratio float64) {
g.price *= (1 - ratio)
g.price *= money(1 - ratio)
}

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

View File

@ -11,13 +11,13 @@ import "fmt"
type puzzle struct {
title string
price float64
price money
}
func (p *puzzle) print() {
fmt.Printf("%-15s: $%.2f \n", p.title, p.price)
fmt.Printf("%-15s: %s\n", p.title, p.price.string())
}
func (p *puzzle) discount(ratio float64) {
p.price *= (1 - ratio)
p.price *= money(1 - ratio)
}

View File

@ -11,15 +11,15 @@ import "fmt"
type book struct {
title string
price float64
price money
readTime int
}
func (b book) print() {
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
fmt.Printf("%-15s: %s\n", b.title, b.price.string())
}
// TODO: NEW
func (b book) sum() float64 {
func (b book) sum() money {
return b.price
}

View File

@ -11,12 +11,12 @@ import "fmt"
type game struct {
title string
price float64
price money
playTime int
}
func (g *game) print() {
fmt.Printf("%-15s: $%.2f\n", g.title, g.price)
fmt.Printf("%-15s: %s\n", g.title, g.price.string())
}
// func (g *game) String() string {
@ -24,10 +24,10 @@ func (g *game) print() {
// }
func (g *game) discount(ratio float64) {
g.price *= (1 - ratio)
g.price *= money(1 - ratio)
}
// TODO: NEW
func (g *game) sum() float64 {
func (g *game) sum() money {
return g.price
}

View File

@ -26,7 +26,7 @@ func (l list) print() {
}
// TODO: NEW
func (l list) sum() (n float64) {
func (l list) sum() (n money) {
for _, it := range l {
n += it.sum()
}

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

View File

@ -11,14 +11,14 @@ import "fmt"
type puzzle struct {
title string
price float64
price money
}
func (p puzzle) print() {
fmt.Printf("%-15s: $%.2f \n", p.title, p.price)
fmt.Printf("%-15s: %s\n", p.title, p.price.string())
}
// TODO: NEW
func (p puzzle) sum() float64 {
func (p puzzle) sum() money {
return p.price
}

View File

@ -10,7 +10,7 @@ type printer interface {
// TODO: NEW
type summer interface {
sum() float64
sum() money
}
// TODO: NEW