diff --git a/interfaces/01-methods/main.go b/interfaces/01-methods/main.go index dc3a19b..0b751ab 100644 --- a/interfaces/01-methods/main.go +++ b/interfaces/01-methods/main.go @@ -7,8 +7,6 @@ package main -import "fmt" - func main() { mobydick := book{ title: "moby dick", @@ -25,12 +23,6 @@ func main() { 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 mobydick.print() // sends `mobydick` value to `book.print` minecraft.print() // sends `minecraft` value to `game.print` diff --git a/interfaces/02-receivers/book.go b/interfaces/02-receivers/book.go index 7b925aa..2d01091 100644 --- a/interfaces/02-receivers/book.go +++ b/interfaces/02-receivers/book.go @@ -15,6 +15,5 @@ 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) } diff --git a/interfaces/02-receivers/game.go b/interfaces/02-receivers/game.go index dcaf1cf..0dc2b3b 100644 --- a/interfaces/02-receivers/game.go +++ b/interfaces/02-receivers/game.go @@ -14,6 +14,9 @@ type game struct { 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() { fmt.Printf("%-15s: $%.2f\n", g.title, g.price) } @@ -27,8 +30,7 @@ func (g *game) discount(ratio float64) { // PREVIOUS CODE: -// ---------------------------------------------------------------------------- // + `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) // } diff --git a/interfaces/02-receivers/huge.go b/interfaces/02-receivers/huge.go new file mode 100644 index 0000000..340a2c1 --- /dev/null +++ b/interfaces/02-receivers/huge.go @@ -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) +// } diff --git a/interfaces/02-receivers/main.go b/interfaces/02-receivers/main.go index deba14e..e4ecb30 100644 --- a/interfaces/02-receivers/main.go +++ b/interfaces/02-receivers/main.go @@ -9,19 +9,22 @@ package main func main() { var ( - // mobydick = book{title: "moby dick", price: 10} + 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) + // sends a pointer of minecraft to the discount method + // same as: (&minecraft).discount(.1) + minecraft.discount(.1) - // you can attach methods to a compatible type on the fly: - my := list([]game{minecraft, tetris}) + mobydick.print() + minecraft.print() + tetris.print() - // you can call methods even on a nil value - // my = nil - - my.print() // or: list.print(items) + // creates a variable that occupies 200mb on memory + var h huge + for i := 0; i < 10; i++ { + h.addr() + } } diff --git a/interfaces/03-nonstructs/book.go b/interfaces/03-nonstructs/book.go new file mode 100644 index 0000000..7b925aa --- /dev/null +++ b/interfaces/03-nonstructs/book.go @@ -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) +} diff --git a/interfaces/03-nonstructs/game.go b/interfaces/03-nonstructs/game.go new file mode 100644 index 0000000..dcaf1cf --- /dev/null +++ b/interfaces/03-nonstructs/game.go @@ -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) +// } diff --git a/interfaces/02-receivers/list.go b/interfaces/03-nonstructs/list.go similarity index 100% rename from interfaces/02-receivers/list.go rename to interfaces/03-nonstructs/list.go diff --git a/interfaces/03-nonstructs/main.go b/interfaces/03-nonstructs/main.go new file mode 100644 index 0000000..deba14e --- /dev/null +++ b/interfaces/03-nonstructs/main.go @@ -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) +} diff --git a/interfaces/03-interfaces/book.go b/interfaces/04-interfaces/book.go similarity index 100% rename from interfaces/03-interfaces/book.go rename to interfaces/04-interfaces/book.go diff --git a/interfaces/03-interfaces/game.go b/interfaces/04-interfaces/game.go similarity index 100% rename from interfaces/03-interfaces/game.go rename to interfaces/04-interfaces/game.go diff --git a/interfaces/03-interfaces/list.go b/interfaces/04-interfaces/list.go similarity index 100% rename from interfaces/03-interfaces/list.go rename to interfaces/04-interfaces/list.go diff --git a/interfaces/03-interfaces/main.go b/interfaces/04-interfaces/main.go similarity index 100% rename from interfaces/03-interfaces/main.go rename to interfaces/04-interfaces/main.go diff --git a/interfaces/03-interfaces/puzzle.go b/interfaces/04-interfaces/puzzle.go similarity index 100% rename from interfaces/03-interfaces/puzzle.go rename to interfaces/04-interfaces/puzzle.go diff --git a/interfaces/04-assertion/book.go b/interfaces/05-assertion/book.go similarity index 100% rename from interfaces/04-assertion/book.go rename to interfaces/05-assertion/book.go diff --git a/interfaces/04-assertion/game.go b/interfaces/05-assertion/game.go similarity index 100% rename from interfaces/04-assertion/game.go rename to interfaces/05-assertion/game.go diff --git a/interfaces/04-assertion/list.go b/interfaces/05-assertion/list.go similarity index 100% rename from interfaces/04-assertion/list.go rename to interfaces/05-assertion/list.go diff --git a/interfaces/04-assertion/main.go b/interfaces/05-assertion/main.go similarity index 100% rename from interfaces/04-assertion/main.go rename to interfaces/05-assertion/main.go diff --git a/interfaces/04-assertion/puzzle.go b/interfaces/05-assertion/puzzle.go similarity index 100% rename from interfaces/04-assertion/puzzle.go rename to interfaces/05-assertion/puzzle.go diff --git a/interfaces/05-composition/book.go b/interfaces/06-composition/book.go similarity index 100% rename from interfaces/05-composition/book.go rename to interfaces/06-composition/book.go diff --git a/interfaces/05-composition/discount.go b/interfaces/06-composition/discount.go similarity index 100% rename from interfaces/05-composition/discount.go rename to interfaces/06-composition/discount.go diff --git a/interfaces/05-composition/game.go b/interfaces/06-composition/game.go similarity index 100% rename from interfaces/05-composition/game.go rename to interfaces/06-composition/game.go diff --git a/interfaces/05-composition/list.go b/interfaces/06-composition/list.go similarity index 100% rename from interfaces/05-composition/list.go rename to interfaces/06-composition/list.go diff --git a/interfaces/05-composition/main.go b/interfaces/06-composition/main.go similarity index 100% rename from interfaces/05-composition/main.go rename to interfaces/06-composition/main.go diff --git a/interfaces/05-composition/puzzle.go b/interfaces/06-composition/puzzle.go similarity index 100% rename from interfaces/05-composition/puzzle.go rename to interfaces/06-composition/puzzle.go diff --git a/interfaces/05-composition/time.go b/interfaces/06-composition/time.go similarity index 100% rename from interfaces/05-composition/time.go rename to interfaces/06-composition/time.go diff --git a/interfaces/05-composition/types.go b/interfaces/06-composition/types.go similarity index 100% rename from interfaces/05-composition/types.go rename to interfaces/06-composition/types.go