interfaces: refactor
This commit is contained in:
20
interfaces/02-receivers/book.go
Normal file
20
interfaces/02-receivers/book.go
Normal file
@ -0,0 +1,20 @@
|
||||
// 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 book struct {
|
||||
title string
|
||||
price float64
|
||||
}
|
||||
|
||||
func (b book) print() {
|
||||
// b is a copy of the original `book` value here.
|
||||
fmt.Printf("%-15s: $%.2f \n", b.title, b.price)
|
||||
}
|
34
interfaces/02-receivers/game.go
Normal file
34
interfaces/02-receivers/game.go
Normal file
@ -0,0 +1,34 @@
|
||||
// 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 game struct {
|
||||
title string
|
||||
price float64
|
||||
}
|
||||
|
||||
func (g *game) print() {
|
||||
fmt.Printf("%-15s: $%.2f\n", g.title, g.price)
|
||||
}
|
||||
|
||||
// + 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)
|
||||
}
|
||||
|
||||
// PREVIOUS CODE:
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// + `g` is a copy: `discount` cannot change the original `g`.
|
||||
// func (g game) discount(ratio float64) {
|
||||
// g.price *= (1 - ratio)
|
||||
// }
|
28
interfaces/02-receivers/list.go
Normal file
28
interfaces/02-receivers/list.go
Normal file
@ -0,0 +1,28 @@
|
||||
// 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"
|
||||
|
||||
// + you can attach methods to any concrete type.
|
||||
// + rule: you need to declare a new type in the same package.
|
||||
type list []game
|
||||
|
||||
func (l list) print() {
|
||||
// `list` acts like a `[]game`
|
||||
if len(l) == 0 {
|
||||
fmt.Println("Sorry. Our store is closed. We're waiting for the delivery 🚚.")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("My Store:\n")
|
||||
fmt.Printf("---------\n")
|
||||
for _, it := range l {
|
||||
it.print()
|
||||
}
|
||||
}
|
27
interfaces/02-receivers/main.go
Normal file
27
interfaces/02-receivers/main.go
Normal file
@ -0,0 +1,27 @@
|
||||
// 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
|
||||
|
||||
func main() {
|
||||
var (
|
||||
// mobydick = book{title: "moby dick", price: 10}
|
||||
minecraft = game{title: "minecraft", price: 20}
|
||||
tetris = game{title: "tetris", price: 5}
|
||||
)
|
||||
|
||||
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
|
||||
// my = nil
|
||||
|
||||
my.print() // or: list.print(items)
|
||||
}
|
Reference in New Issue
Block a user