add: money type to interfaces
This commit is contained in:
@ -16,7 +16,7 @@ type book struct {
|
|||||||
|
|
||||||
func (b book) print() {
|
func (b book) print() {
|
||||||
// b is a copy of the original `book` value here.
|
// 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() {
|
// func (b book) printBook() {
|
||||||
// // b is a copy of the original `book` value here.
|
// // 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.
|
// b is a copy of the original `book` value here.
|
||||||
//
|
//
|
||||||
// func printBook(b book) {
|
// func printBook(b book) {
|
||||||
// fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
|
// fmt.Printf("%-15s: $%.2f\n", b.title, b.price)
|
||||||
// }
|
// }
|
||||||
|
@ -15,5 +15,5 @@ type book struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b book) print() {
|
func (b book) print() {
|
||||||
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
|
fmt.Printf("%-15s: $%.2f\n", b.title, b.price)
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,9 @@ import "fmt"
|
|||||||
|
|
||||||
type book struct {
|
type book struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b book) print() {
|
func (b book) print() {
|
||||||
// b is a copy of the original `book` value here.
|
fmt.Printf("%-15s: %s\n", b.title, b.price.string())
|
||||||
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
|
|
||||||
}
|
}
|
||||||
|
@ -11,24 +11,13 @@ import "fmt"
|
|||||||
|
|
||||||
type game struct {
|
type game struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *game) print() {
|
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) {
|
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)
|
|
||||||
// }
|
|
||||||
|
@ -9,9 +9,9 @@ package main
|
|||||||
|
|
||||||
import "fmt"
|
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.
|
// + rule: you need to declare a new type in the same package.
|
||||||
type list []game
|
type list []*game
|
||||||
|
|
||||||
func (l list) print() {
|
func (l list) print() {
|
||||||
// `list` acts like a `[]game`
|
// `list` acts like a `[]game`
|
||||||
|
@ -14,14 +14,15 @@ func main() {
|
|||||||
tetris = game{title: "tetris", price: 5}
|
tetris = game{title: "tetris", price: 5}
|
||||||
)
|
)
|
||||||
|
|
||||||
var items []game
|
var items []*game
|
||||||
items = append(items, minecraft, tetris)
|
items = append(items, &minecraft, &tetris)
|
||||||
|
|
||||||
// you can attach methods to a compatible type on the fly:
|
// you can attach methods to a compatible type on the fly:
|
||||||
my := list([]game{minecraft, tetris})
|
// items -> []*game
|
||||||
|
// list -> []*game
|
||||||
// you can call methods even on a nil value
|
my := list(items)
|
||||||
// my = nil
|
// my = nil
|
||||||
|
|
||||||
my.print() // or: list.print(items)
|
// you can call methods even on a nil value
|
||||||
|
my.print()
|
||||||
}
|
}
|
||||||
|
17
interfaces/03-nonstructs/money.go
Normal file
17
interfaces/03-nonstructs/money.go
Normal 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)
|
||||||
|
}
|
@ -11,9 +11,9 @@ import "fmt"
|
|||||||
|
|
||||||
type book struct {
|
type book struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b book) print() {
|
func (b book) print() {
|
||||||
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
|
fmt.Printf("%-15s: %s\n", b.title, b.price.string())
|
||||||
}
|
}
|
||||||
|
@ -11,13 +11,13 @@ import "fmt"
|
|||||||
|
|
||||||
type game struct {
|
type game struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *game) print() {
|
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) {
|
func (g *game) discount(ratio float64) {
|
||||||
g.price *= (1 - ratio)
|
g.price *= money(1 - ratio)
|
||||||
}
|
}
|
16
interfaces/04-interfaces/money.go
Normal file
16
interfaces/04-interfaces/money.go
Normal 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)
|
||||||
|
}
|
@ -11,9 +11,9 @@ import "fmt"
|
|||||||
|
|
||||||
type puzzle struct {
|
type puzzle struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p puzzle) print() {
|
func (p puzzle) print() {
|
||||||
fmt.Printf("%-15s: $%.2f \n", p.title, p.price)
|
fmt.Printf("%-15s: %s\n", p.title, p.price.string())
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,10 @@ import "fmt"
|
|||||||
|
|
||||||
type book struct {
|
type book struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
readTime int
|
readTime int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b book) print() {
|
func (b book) print() {
|
||||||
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
|
fmt.Printf("%-15s: %s\n", b.title, b.price.string())
|
||||||
}
|
}
|
@ -11,14 +11,14 @@ import "fmt"
|
|||||||
|
|
||||||
type game struct {
|
type game struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
playTime int
|
playTime int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *game) print() {
|
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) {
|
func (g *game) discount(ratio float64) {
|
||||||
g.price *= (1 - ratio)
|
g.price *= money(1 - ratio)
|
||||||
}
|
}
|
16
interfaces/05-assertion/money.go
Normal file
16
interfaces/05-assertion/money.go
Normal 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)
|
||||||
|
}
|
@ -11,13 +11,13 @@ import "fmt"
|
|||||||
|
|
||||||
type puzzle struct {
|
type puzzle struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *puzzle) print() {
|
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) {
|
func (p *puzzle) discount(ratio float64) {
|
||||||
p.price *= (1 - ratio)
|
p.price *= money(1 - ratio)
|
||||||
}
|
}
|
||||||
|
@ -11,15 +11,15 @@ import "fmt"
|
|||||||
|
|
||||||
type book struct {
|
type book struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
readTime int
|
readTime int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b book) print() {
|
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
|
// TODO: NEW
|
||||||
func (b book) sum() float64 {
|
func (b book) sum() money {
|
||||||
return b.price
|
return b.price
|
||||||
}
|
}
|
||||||
|
@ -11,12 +11,12 @@ import "fmt"
|
|||||||
|
|
||||||
type game struct {
|
type game struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
playTime int
|
playTime int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *game) print() {
|
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 {
|
// func (g *game) String() string {
|
||||||
@ -24,10 +24,10 @@ func (g *game) print() {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
func (g *game) discount(ratio float64) {
|
func (g *game) discount(ratio float64) {
|
||||||
g.price *= (1 - ratio)
|
g.price *= money(1 - ratio)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: NEW
|
// TODO: NEW
|
||||||
func (g *game) sum() float64 {
|
func (g *game) sum() money {
|
||||||
return g.price
|
return g.price
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ func (l list) print() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: NEW
|
// TODO: NEW
|
||||||
func (l list) sum() (n float64) {
|
func (l list) sum() (n money) {
|
||||||
for _, it := range l {
|
for _, it := range l {
|
||||||
n += it.sum()
|
n += it.sum()
|
||||||
}
|
}
|
||||||
|
16
interfaces/06-composition/money.go
Normal file
16
interfaces/06-composition/money.go
Normal 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)
|
||||||
|
}
|
@ -11,14 +11,14 @@ import "fmt"
|
|||||||
|
|
||||||
type puzzle struct {
|
type puzzle struct {
|
||||||
title string
|
title string
|
||||||
price float64
|
price money
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p puzzle) print() {
|
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
|
// TODO: NEW
|
||||||
func (p puzzle) sum() float64 {
|
func (p puzzle) sum() money {
|
||||||
return p.price
|
return p.price
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ type printer interface {
|
|||||||
|
|
||||||
// TODO: NEW
|
// TODO: NEW
|
||||||
type summer interface {
|
type summer interface {
|
||||||
sum() float64
|
sum() money
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: NEW
|
// TODO: NEW
|
||||||
|
Reference in New Issue
Block a user