From 59865eac283b416a911d2aa9ca00e16d5e7da226 Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Mon, 21 Oct 2019 12:47:04 +0300 Subject: [PATCH] add: summaries iface --- interfaces/09-little-refactor/main.go | 15 +++++++++++++++ interfaces/10-stringer/main.go | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/interfaces/09-little-refactor/main.go b/interfaces/09-little-refactor/main.go index 5561cf3..726fe8d 100644 --- a/interfaces/09-little-refactor/main.go +++ b/interfaces/09-little-refactor/main.go @@ -17,3 +17,18 @@ func main() { l.discount(.5) l.print() } + +/* +Summary: + +- Prefer to work directly with concrete types + - Leads to a simple and easy to understand code + - Abstractions (interfaces) can unnecessarily complicate your code + +- Separating responsibilities is critical + - Timestamp type can represent, store, and print a UNIX timestamp + +- When a type anonymously embeds a type, it can use the methods of the embedded type as its own. + - Timestamp embeds a time.Time + - So you can call the methods of the time.Time through a timestamp value +*/ diff --git a/interfaces/10-stringer/main.go b/interfaces/10-stringer/main.go index be85bef..7e114a6 100644 --- a/interfaces/10-stringer/main.go +++ b/interfaces/10-stringer/main.go @@ -32,3 +32,17 @@ func main() { // var pocket money = 10 // fmt.Println("I have", pocket) } + +/* +Summary: + +- fmt.Stringer has one method: String() + - That returns a string. + - It is better to be an fmt.Stringer instead of printing directly. + +- Implement the String() on a type and the type can represent itself as a string. + - Bonus: The functions in the fmt package can print your type. + - They use type assertion to detect if a type implements a String() method. + +- strings.Builder can efficiently combine multiple string values. +*/