reorganize: interfaces section
This commit is contained in:
@ -7,8 +7,6 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mobydick := book{
|
mobydick := book{
|
||||||
title: "moby dick",
|
title: "moby dick",
|
||||||
@ -25,12 +23,6 @@ func main() {
|
|||||||
price: 5,
|
price: 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
// #4: method expressions
|
|
||||||
// methods are just functions that receive a value of a type.
|
|
||||||
game.print(minecraft) // sends `minecraft` value to `game.print`
|
|
||||||
game.print(tetris) // sends `tetris` value to `game.print`
|
|
||||||
fmt.Println()
|
|
||||||
|
|
||||||
// #3
|
// #3
|
||||||
mobydick.print() // sends `mobydick` value to `book.print`
|
mobydick.print() // sends `mobydick` value to `book.print`
|
||||||
minecraft.print() // sends `minecraft` value to `game.print`
|
minecraft.print() // sends `minecraft` value to `game.print`
|
||||||
|
@ -15,6 +15,5 @@ type book struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b book) print() {
|
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)
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,9 @@ type game struct {
|
|||||||
price float64
|
price float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// be consistent:
|
||||||
|
// if another method in the same type has a pointer receiver,
|
||||||
|
// use pointer receivers on the other methods as well.
|
||||||
func (g *game) print() {
|
func (g *game) print() {
|
||||||
fmt.Printf("%-15s: $%.2f\n", g.title, g.price)
|
fmt.Printf("%-15s: $%.2f\n", g.title, g.price)
|
||||||
}
|
}
|
||||||
@ -27,8 +30,7 @@ func (g *game) discount(ratio float64) {
|
|||||||
|
|
||||||
// PREVIOUS CODE:
|
// PREVIOUS CODE:
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
// + `g` is a copy: `discount` cannot change the original `g`.
|
// + `g` is a copy: `discount` cannot change the original `g`.
|
||||||
// func (g game) discount(ratio float64) {
|
// func (g *game) discount(ratio float64) {
|
||||||
// g.price *= (1 - ratio)
|
// g.price *= (1 - ratio)
|
||||||
// }
|
// }
|
||||||
|
29
interfaces/02-receivers/huge.go
Normal file
29
interfaces/02-receivers/huge.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// 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 huge struct {
|
||||||
|
// about ~200mb
|
||||||
|
games [10000000]game
|
||||||
|
}
|
||||||
|
|
||||||
|
// only copies a single pointer.
|
||||||
|
func (h *huge) addr() {
|
||||||
|
fmt.Printf("%p\n", h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// each time it is called,
|
||||||
|
// the original value will be copied.
|
||||||
|
// calling addr() x 10 times = ~2 GB.
|
||||||
|
// func (h huge) addr() {
|
||||||
|
// fmt.Printf("%p\n", &h)
|
||||||
|
// }
|
@ -9,19 +9,22 @@ package main
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
// mobydick = book{title: "moby dick", price: 10}
|
mobydick = book{title: "moby dick", price: 10}
|
||||||
minecraft = game{title: "minecraft", price: 20}
|
minecraft = game{title: "minecraft", price: 20}
|
||||||
tetris = game{title: "tetris", price: 5}
|
tetris = game{title: "tetris", price: 5}
|
||||||
)
|
)
|
||||||
|
|
||||||
var items []game
|
// sends a pointer of minecraft to the discount method
|
||||||
items = append(items, minecraft, tetris)
|
// same as: (&minecraft).discount(.1)
|
||||||
|
minecraft.discount(.1)
|
||||||
|
|
||||||
// you can attach methods to a compatible type on the fly:
|
mobydick.print()
|
||||||
my := list([]game{minecraft, tetris})
|
minecraft.print()
|
||||||
|
tetris.print()
|
||||||
|
|
||||||
// you can call methods even on a nil value
|
// creates a variable that occupies 200mb on memory
|
||||||
// my = nil
|
var h huge
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
my.print() // or: list.print(items)
|
h.addr()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
20
interfaces/03-nonstructs/book.go
Normal file
20
interfaces/03-nonstructs/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/03-nonstructs/game.go
Normal file
34
interfaces/03-nonstructs/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)
|
||||||
|
// }
|
27
interfaces/03-nonstructs/main.go
Normal file
27
interfaces/03-nonstructs/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