diff --git a/interfaces/_old/09-stringer/_handlemethods.go b/interfaces/_old/09-stringer/_handlemethods.go deleted file mode 100644 index e6f362b..0000000 --- a/interfaces/_old/09-stringer/_handlemethods.go +++ /dev/null @@ -1,46 +0,0 @@ -// 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/ -// - -// In the depths of the Go standard library's fmt package... -// Printing functions use the handleMethods method. - -// Example: -// var pocket money = 10 -// fmt.Println(pocket) - -// the argument can be any type of value -// stores the pocket variable in the argument variable -// ^ -// | -func (p *pp) handleMethods(argument interface{}) (handled bool) { - // ... - - // Checks whether a given argument is an error or an fmt.Stringer - switch v := argument.(type) { - // ... - // If the argument is a Stringer, calls its String() method - case Stringer: - // ... - // pocket.String() - // ^ - // | - p.fmtString(v.String(), verb) - return - } - - // ... -} - -/* -The original `handleMethods` code is more involved: - -https://github.com/golang/go/blob/6f51082da77a1d4cafd5b7af0db69293943f4066/src/fmt/print.go#L574 - - -> 574#handleMethods(..) - -> 627#Stringer type check: If `v` is a Stringer, run: - -> 630#v.String() -*/ \ No newline at end of file diff --git a/interfaces/_old/09-stringer/book.go b/interfaces/_old/09-stringer/book.go deleted file mode 100644 index 75ba2c8..0000000 --- a/interfaces/_old/09-stringer/book.go +++ /dev/null @@ -1,30 +0,0 @@ -// 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 { - product - published timestamp -} - -// String method makes the book an fmt.Stringer. -// The list was calling the embedded product type's String(), -// now it calls the book.String(). -func (b *book) String() string { - // product.String() has a pointer receiver. - // Therefore, you need to manually take the product's address. - // - // If you pass: "b.product", Go would pass it as a copy to `Sprintf`. - // In that case, Go can't deference b.product automatically. - // It's because: b.product would be a different value, a copy. - return fmt.Sprintf("%s - (%s)", &b.product, b.published) -} diff --git a/interfaces/_old/09-stringer/game.go b/interfaces/_old/09-stringer/game.go deleted file mode 100644 index a73291b..0000000 --- a/interfaces/_old/09-stringer/game.go +++ /dev/null @@ -1,15 +0,0 @@ -// 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 - -type game struct { - // game is an fmt.Stringer - // because the product is an fmt.Stringer - // and the game embeds the product - product -} diff --git a/interfaces/_old/09-stringer/list.go b/interfaces/_old/09-stringer/list.go deleted file mode 100644 index 92907a9..0000000 --- a/interfaces/_old/09-stringer/list.go +++ /dev/null @@ -1,60 +0,0 @@ -// 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" - "strings" -) - -type item interface { - discount(ratio float64) - - // ~~ interface embedding ~~ - // - // item interface embeds the fmt.Stringer interface. - // - // Go adds the methods of the fmt.Stringer - // to this item interface. - // - // same as this: - // String() string - // fmt.Stringer -} -type list []item - -// String method makes the list an fmt.Stringer. -func (l list) String() string { - if len(l) == 0 { - return "Sorry. We're waiting for delivery 🚚." - } - - // use the strings.Builder when you're combining - // a long list of strings together. - var str strings.Builder - - for _, it := range l { - // the builder.WriteString doesn't know about the stringer interface. - // because it takes a string argument. - // so, you need to call the String method yourself. - // str.WriteString(it.String()) - // str.WriteRune('\n') - - // or slower way: - // Print funcs can detect fmt.Stringer automatically. - fmt.Fprintln(&str, it) - } - - return str.String() -} - -func (l list) discount(ratio float64) { - for _, it := range l { - it.discount(ratio) - } -} diff --git a/interfaces/_old/09-stringer/main.go b/interfaces/_old/09-stringer/main.go deleted file mode 100644 index 3708391..0000000 --- a/interfaces/_old/09-stringer/main.go +++ /dev/null @@ -1,38 +0,0 @@ -// 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" - -func main() { - // The money type is a stringer. - // You don't need to call the String method when printing a value of it. - // var pocket money = 10 - // fmt.Println(pocket) - - store := list{ - &book{product{"moby dick", 10}, toTimestamp(118281600)}, - &book{product{"odyssey", 15}, toTimestamp("733622400")}, - &book{product{"hobbit", 25}, toTimestamp(nil)}, - &puzzle{product{"rubik's cube", 5}}, - &game{product{"minecraft", 20}}, - &game{product{"tetris", 5}}, - &toy{product{"yoda", 150}}, - } - - store.discount(.5) - - // The list is a stringer. - // The `fmt.Print` function can print the `store` - // by calling `store`'s `String()` method. - // - // Underneath, `fmt.Print` uses a type switch to - // detect whether a type is a Stringer: - // https://golang.org/src/fmt/print.go#L627 - fmt.Print(store) -} diff --git a/interfaces/_old/09-stringer/money.go b/interfaces/_old/09-stringer/money.go deleted file mode 100644 index aa661f9..0000000 --- a/interfaces/_old/09-stringer/money.go +++ /dev/null @@ -1,17 +0,0 @@ -// 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 - -// String makes the money an fmt.Stringer -func (m money) String() string { - return fmt.Sprintf("$%.2f", m) -} diff --git a/interfaces/_old/09-stringer/product.go b/interfaces/_old/09-stringer/product.go deleted file mode 100644 index 08b7449..0000000 --- a/interfaces/_old/09-stringer/product.go +++ /dev/null @@ -1,24 +0,0 @@ -// 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 product struct { - title string - price money -} - -// String makes the product an fmt.Stringer -func (p *product) String() string { - return fmt.Sprintf("%-15s: %s", p.title, p.price) -} - -func (p *product) discount(ratio float64) { - p.price *= money(1 - ratio) -} diff --git a/interfaces/_old/09-stringer/puzzle.go b/interfaces/_old/09-stringer/puzzle.go deleted file mode 100644 index 2e4572d..0000000 --- a/interfaces/_old/09-stringer/puzzle.go +++ /dev/null @@ -1,15 +0,0 @@ -// 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 - -type puzzle struct { - // puzzle is an fmt.Stringer - // because the product is an fmt.Stringer - // and the puzzle embeds the product - product -} diff --git a/interfaces/_old/09-stringer/timestamp.go b/interfaces/_old/09-stringer/timestamp.go deleted file mode 100644 index 87a4c62..0000000 --- a/interfaces/_old/09-stringer/timestamp.go +++ /dev/null @@ -1,53 +0,0 @@ -// 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 ( - "strconv" - "time" -) - -// timestamp stores, formats and automatically prints a timestamp: it's a stringer. -type timestamp struct { - // timestamp embeds a time, therefore it can be used as a time value. - // there is no need to convert a time value to a timestamp value. - time.Time -} - -// String method makes the timestamp an fmt.stringer. -func (ts timestamp) String() string { - if ts.IsZero() { - return "unknown" - } - - // Mon Jan 2 15:04:05 -0700 MST 2006 - const layout = "2006/01" - return ts.Format(layout) -} - -// toTimestamp returns a timestamp value depending on the type of `v`. -// toTimestamp was "book.format()" before. -func toTimestamp(v interface{}) timestamp { - var t int - - switch v := v.(type) { - case int: - // book{title: "moby dick", price: 10, published: 118281600}, - t = v - case string: - // book{title: "odyssey", price: 15, published: "733622400"}, - t, _ = strconv.Atoi(v) - default: - // book{title: "hobbit", price: 25}, - return timestamp{} - } - - return timestamp{ - Time: time.Unix(int64(t), 0), - } -} diff --git a/interfaces/_old/09-stringer/toy.go b/interfaces/_old/09-stringer/toy.go deleted file mode 100644 index 9d97da4..0000000 --- a/interfaces/_old/09-stringer/toy.go +++ /dev/null @@ -1,15 +0,0 @@ -// 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 - -type toy struct { - // toy is an fmt.Stringer - // because the product is an fmt.Stringer - // and the toy embeds the product - product -} diff --git a/interfaces/_old/10-marshaler/book.go b/interfaces/_old/10-marshaler/book.go deleted file mode 100644 index 9634a16..0000000 --- a/interfaces/_old/10-marshaler/book.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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 { - product - - // Published (timestamp) knows how to print, encode and decode itself. - Published timestamp -} - -func (b *book) String() string { - return fmt.Sprintf("%s - (%s)", &b.product, b.Published) -} diff --git a/interfaces/_old/10-marshaler/game.go b/interfaces/_old/10-marshaler/game.go deleted file mode 100644 index 3604b15..0000000 --- a/interfaces/_old/10-marshaler/game.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type game struct { - product -} diff --git a/interfaces/_old/10-marshaler/list.go b/interfaces/_old/10-marshaler/list.go deleted file mode 100644 index 46f2b01..0000000 --- a/interfaces/_old/10-marshaler/list.go +++ /dev/null @@ -1,39 +0,0 @@ -// 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" - "strings" -) - -type item interface { - discount(ratio float64) - fmt.Stringer -} - -type list []item - -func (l list) String() string { - if len(l) == 0 { - return "Sorry. We're waiting for delivery 🚚." - } - - var str strings.Builder - for _, it := range l { - str.WriteString(it.String()) - str.WriteRune('\n') - } - return str.String() -} - -func (l list) discount(ratio float64) { - for _, it := range l { - it.discount(ratio) - } -} diff --git a/interfaces/_old/10-marshaler/main.go b/interfaces/_old/10-marshaler/main.go deleted file mode 100644 index a206f97..0000000 --- a/interfaces/_old/10-marshaler/main.go +++ /dev/null @@ -1,60 +0,0 @@ -// 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 ( - "encoding/json" - "fmt" - "log" -) - -func main() { - out := encode() - decode(out) -} - -func decode(data []byte) { - // data := []byte(`[ - // { "Title": "moby dick", "Price": 5, "Published": 118281600 }, - // { "Title": "odyssey", "Price": 7.5, "Published": 733622400 }, - // { "Title": "hobbit", "Price": 12.5, "Published": -62135596800 } - // ]`) - - var books []*book - - if err := json.Unmarshal(data, &books); err != nil { - log.Fatalln(err) - } - - for _, b := range books { - fmt.Println(b) - } -} - -func encode() []byte { - store := list{ - &book{product{"moby dick", 10}, toTimestamp(118281600)}, - &book{product{"odyssey", 15}, toTimestamp("733622400")}, - &book{product{"hobbit", 25}, toTimestamp(nil)}, - &puzzle{product{"rubik's cube", 5}}, - &game{product{"minecraft", 20}}, - &game{product{"tetris", 5}}, - &toy{product{"yoda", 150}}, - } - - store.discount(.5) - // fmt.Print(store) - - out, err := json.MarshalIndent(store[:3], "", "\t") - if err != nil { - log.Fatalln(err) - } - - return out - // fmt.Println(string(out)) -} diff --git a/interfaces/_old/10-marshaler/money.go b/interfaces/_old/10-marshaler/money.go deleted file mode 100644 index 095a039..0000000 --- a/interfaces/_old/10-marshaler/money.go +++ /dev/null @@ -1,16 +0,0 @@ -// 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) -} diff --git a/interfaces/_old/10-marshaler/product.go b/interfaces/_old/10-marshaler/product.go deleted file mode 100644 index f93cb84..0000000 --- a/interfaces/_old/10-marshaler/product.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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 product struct { - Title string - Price money -} - -func (p *product) String() string { - return fmt.Sprintf("%-15s: %s", p.Title, p.Price) -} - -func (p *product) discount(ratio float64) { - p.Price *= money(1 - ratio) -} diff --git a/interfaces/_old/10-marshaler/puzzle.go b/interfaces/_old/10-marshaler/puzzle.go deleted file mode 100644 index acbaa47..0000000 --- a/interfaces/_old/10-marshaler/puzzle.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type puzzle struct { - product -} diff --git a/interfaces/_old/10-marshaler/timestamp.go b/interfaces/_old/10-marshaler/timestamp.go deleted file mode 100644 index 04732e6..0000000 --- a/interfaces/_old/10-marshaler/timestamp.go +++ /dev/null @@ -1,64 +0,0 @@ -// 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 ( - "strconv" - "time" -) - -type timestamp struct { - time.Time -} - -// timestamp knows how to decode itself from json. -// -// UnmarshalJSON is an implementation of the json.Unmarshaler interface. -// json.Unmarshal and json.Decode call this method. -func (ts *timestamp) UnmarshalJSON(data []byte) error { - *ts = toTimestamp(string(data)) - return nil -} - -// timestamp knows how to encode itself to json. -// -// MarshalJSON is an implementation of the json.Marshaler interface. -// json.Marshal and json.Encode call this method. -func (ts timestamp) MarshalJSON() (out []byte, err error) { - return strconv.AppendInt(out, ts.Unix(), 10), nil -} - -func (ts timestamp) String() string { - if ts.IsZero() { - return "unknown" - } - - // Mon Jan 2 15:04:05 -0700 MST 2006 - const layout = "2006/01" - return ts.Format(layout) -} - -func toTimestamp(v interface{}) timestamp { - var t int - - switch v := v.(type) { - case int: - // book{title: "moby dick", price: 10, published: 118281600}, - t = v - case string: - // book{title: "odyssey", price: 15, published: "733622400"}, - t, _ = strconv.Atoi(v) - default: - // book{title: "hobbit", price: 25}, - return timestamp{} - } - - return timestamp{ - Time: time.Unix(int64(t), 0), - } -} diff --git a/interfaces/_old/10-marshaler/toy.go b/interfaces/_old/10-marshaler/toy.go deleted file mode 100644 index ab5613d..0000000 --- a/interfaces/_old/10-marshaler/toy.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type toy struct { - product -} diff --git a/interfaces/_old/11-decode-toiface/book.go b/interfaces/_old/11-decode-toiface/book.go deleted file mode 100644 index c36372f..0000000 --- a/interfaces/_old/11-decode-toiface/book.go +++ /dev/null @@ -1,21 +0,0 @@ -// 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 { - product - Published timestamp -} - -func (b *book) String() string { - return fmt.Sprintf("%s - (%s)", &b.product, b.Published) -} diff --git a/interfaces/_old/11-decode-toiface/database.go b/interfaces/_old/11-decode-toiface/database.go deleted file mode 100644 index f84ecb0..0000000 --- a/interfaces/_old/11-decode-toiface/database.go +++ /dev/null @@ -1,62 +0,0 @@ -// 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 ( - "encoding/json" - "fmt" -) - -// database is responsible for encoding and decoding a list. -type database struct { - list *list -} - -func (db *database) UnmarshalJSON(data []byte) error { - // Provides a concrete data structure that we can use to decode. - // It is a slice of structs. - var decodables []struct { - Type string // The product type. - Item json.RawMessage // The raw item data (from database.json) - } - - // First, decode the "Type" part. - // Leave the "Item" as raw json data. - if err := json.Unmarshal(data, &decodables); err != nil { - return err - } - - // Decode the "Item" part for each json object. - for _, d := range decodables { - it, err := db.newItem(d.Item, d.Type) - if err != nil { - return err - } - - *db.list = append(*db.list, it) - } - - return nil -} - -// newItem decodes and returns a product type wrapped in an item iface value. -func (*database) newItem(data []byte, itemType string) (it item, _ error) { - switch itemType { - default: - return nil, fmt.Errorf("newItem: type (%q) does not exist", itemType) - case "book": - it = new(book) - case "puzzle": - it = new(puzzle) - case "game": - it = new(game) - case "toy": - it = new(toy) - } - return it, json.Unmarshal(data, &it) -} diff --git a/interfaces/_old/11-decode-toiface/database.json b/interfaces/_old/11-decode-toiface/database.json deleted file mode 100644 index cccab62..0000000 --- a/interfaces/_old/11-decode-toiface/database.json +++ /dev/null @@ -1,54 +0,0 @@ -[ - { - "Type": "book", - "Item": { - "Title": "moby dick", - "Price": 10, - "Published": 118281600 - } - }, - { - "Type": "book", - "Item": { - "Title": "odyssey", - "Price": 15, - "Published": 733622400 - } - }, - { - "Type": "book", - "Item": { - "Title": "hobbit", - "Price": 25, - "Published": -62135596800 - } - }, - { - "Type": "puzzle", - "Item": { - "Title": "rubik's cube", - "Price": 5 - } - }, - { - "Type": "game", - "Item": { - "Title": "minecraft", - "Price": 20 - } - }, - { - "Type": "game", - "Item": { - "Title": "tetris", - "Price": 5 - } - }, - { - "Type": "toy", - "Item": { - "Title": "yoda", - "Price": 150 - } - } -] diff --git a/interfaces/_old/11-decode-toiface/game.go b/interfaces/_old/11-decode-toiface/game.go deleted file mode 100644 index 3604b15..0000000 --- a/interfaces/_old/11-decode-toiface/game.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type game struct { - product -} diff --git a/interfaces/_old/11-decode-toiface/list.go b/interfaces/_old/11-decode-toiface/list.go deleted file mode 100644 index 01b6eaa..0000000 --- a/interfaces/_old/11-decode-toiface/list.go +++ /dev/null @@ -1,40 +0,0 @@ -// 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" - "strings" -) - -type item interface { - discount(ratio float64) - fmt.Stringer -} - -type list []item - -func (l list) String() string { - if len(l) == 0 { - return "Sorry. We're waiting for delivery 🚚." - } - - var str strings.Builder - for _, it := range l { - str.WriteString(it.String()) - str.WriteRune('\n') - } - - return str.String() -} - -func (l list) discount(ratio float64) { - for _, it := range l { - it.discount(ratio) - } -} diff --git a/interfaces/_old/11-decode-toiface/main.go b/interfaces/_old/11-decode-toiface/main.go deleted file mode 100644 index 5aae9cc..0000000 --- a/interfaces/_old/11-decode-toiface/main.go +++ /dev/null @@ -1,54 +0,0 @@ -// 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 ( - "encoding/json" - "fmt" - "io/ioutil" - "log" -) - -func main() { - data, err := ioutil.ReadFile("database.json") - if err != nil { - log.Fatalln(err) - } - - var store list - - db := database{list: &store} - - if err := json.Unmarshal(data, &db); err != nil { - log.Fatalln(err) - } - - fmt.Print(store) - - /* - store := list{ - &book{product{"moby dick", 10}, toTimestamp(118281600)}, - &book{product{"odyssey", 15}, toTimestamp("733622400")}, - &book{product{"hobbit", 25}, unknown}, - &puzzle{product{"rubik's cube", 5}}, - &game{product{"minecraft", 20}}, - &game{product{"tetris", 5}}, - &toy{product{"yoda", 150}}, - } - - out, err := json.MarshalIndent(store, "", "\t") - if err != nil { - log.Fatalln(err) - } - - fmt.Println(string(out)) - - // store.discount(.5) - // fmt.Print(store) - */ -} diff --git a/interfaces/_old/11-decode-toiface/money.go b/interfaces/_old/11-decode-toiface/money.go deleted file mode 100644 index 095a039..0000000 --- a/interfaces/_old/11-decode-toiface/money.go +++ /dev/null @@ -1,16 +0,0 @@ -// 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) -} diff --git a/interfaces/_old/11-decode-toiface/product.go b/interfaces/_old/11-decode-toiface/product.go deleted file mode 100644 index f93cb84..0000000 --- a/interfaces/_old/11-decode-toiface/product.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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 product struct { - Title string - Price money -} - -func (p *product) String() string { - return fmt.Sprintf("%-15s: %s", p.Title, p.Price) -} - -func (p *product) discount(ratio float64) { - p.Price *= money(1 - ratio) -} diff --git a/interfaces/_old/11-decode-toiface/puzzle.go b/interfaces/_old/11-decode-toiface/puzzle.go deleted file mode 100644 index acbaa47..0000000 --- a/interfaces/_old/11-decode-toiface/puzzle.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type puzzle struct { - product -} diff --git a/interfaces/_old/11-decode-toiface/timestamp.go b/interfaces/_old/11-decode-toiface/timestamp.go deleted file mode 100644 index 04732e6..0000000 --- a/interfaces/_old/11-decode-toiface/timestamp.go +++ /dev/null @@ -1,64 +0,0 @@ -// 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 ( - "strconv" - "time" -) - -type timestamp struct { - time.Time -} - -// timestamp knows how to decode itself from json. -// -// UnmarshalJSON is an implementation of the json.Unmarshaler interface. -// json.Unmarshal and json.Decode call this method. -func (ts *timestamp) UnmarshalJSON(data []byte) error { - *ts = toTimestamp(string(data)) - return nil -} - -// timestamp knows how to encode itself to json. -// -// MarshalJSON is an implementation of the json.Marshaler interface. -// json.Marshal and json.Encode call this method. -func (ts timestamp) MarshalJSON() (out []byte, err error) { - return strconv.AppendInt(out, ts.Unix(), 10), nil -} - -func (ts timestamp) String() string { - if ts.IsZero() { - return "unknown" - } - - // Mon Jan 2 15:04:05 -0700 MST 2006 - const layout = "2006/01" - return ts.Format(layout) -} - -func toTimestamp(v interface{}) timestamp { - var t int - - switch v := v.(type) { - case int: - // book{title: "moby dick", price: 10, published: 118281600}, - t = v - case string: - // book{title: "odyssey", price: 15, published: "733622400"}, - t, _ = strconv.Atoi(v) - default: - // book{title: "hobbit", price: 25}, - return timestamp{} - } - - return timestamp{ - Time: time.Unix(int64(t), 0), - } -} diff --git a/interfaces/_old/11-decode-toiface/toy.go b/interfaces/_old/11-decode-toiface/toy.go deleted file mode 100644 index ab5613d..0000000 --- a/interfaces/_old/11-decode-toiface/toy.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type toy struct { - product -} diff --git a/interfaces/_old/12-reflection/book.go b/interfaces/_old/12-reflection/book.go deleted file mode 100644 index c36372f..0000000 --- a/interfaces/_old/12-reflection/book.go +++ /dev/null @@ -1,21 +0,0 @@ -// 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 { - product - Published timestamp -} - -func (b *book) String() string { - return fmt.Sprintf("%s - (%s)", &b.product, b.Published) -} diff --git a/interfaces/_old/12-reflection/database.go b/interfaces/_old/12-reflection/database.go deleted file mode 100644 index 1d22b70..0000000 --- a/interfaces/_old/12-reflection/database.go +++ /dev/null @@ -1,82 +0,0 @@ -// 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 ( - "encoding/json" - "fmt" - "reflect" -) - -type database struct { - list *list -} - -func (db *database) MarshalJSON() ([]byte, error) { - type encodable struct { - Type string - Item item - } - - var e []encodable - - for _, it := range *db.list { - // TypeOf -> finds the dynamic type of "it" - // Elem -> returns the element type of the pointer - // Name -> returns the type name as string - t := reflect.TypeOf(it).Elem().Name() - e = append(e, encodable{t, it}) - - // Another way: - // - // t := fmt.Sprintf("%T", it) - // - // Uses: reflect.TypeOf(arg).String() - } - - return json.Marshal(e) -} - -func (db *database) UnmarshalJSON(data []byte) error { - var decodables []struct { - Type string - Item json.RawMessage - } - - if err := json.Unmarshal(data, &decodables); err != nil { - return err - } - - for _, d := range decodables { - it, err := db.newItem(d.Item, d.Type) - if err != nil { - return err - } - - *db.list = append(*db.list, it) - } - - return nil -} - -// newItem decodes and returns a product type wrapped in an item iface value. -func (*database) newItem(data []byte, itemType string) (it item, _ error) { - switch itemType { - default: - return nil, fmt.Errorf("newItem: type (%q) does not exist", itemType) - case "book": - it = new(book) - case "puzzle": - it = new(puzzle) - case "game": - it = new(game) - case "toy": - it = new(toy) - } - return it, json.Unmarshal(data, &it) -} diff --git a/interfaces/_old/12-reflection/database.json b/interfaces/_old/12-reflection/database.json deleted file mode 100644 index cccab62..0000000 --- a/interfaces/_old/12-reflection/database.json +++ /dev/null @@ -1,54 +0,0 @@ -[ - { - "Type": "book", - "Item": { - "Title": "moby dick", - "Price": 10, - "Published": 118281600 - } - }, - { - "Type": "book", - "Item": { - "Title": "odyssey", - "Price": 15, - "Published": 733622400 - } - }, - { - "Type": "book", - "Item": { - "Title": "hobbit", - "Price": 25, - "Published": -62135596800 - } - }, - { - "Type": "puzzle", - "Item": { - "Title": "rubik's cube", - "Price": 5 - } - }, - { - "Type": "game", - "Item": { - "Title": "minecraft", - "Price": 20 - } - }, - { - "Type": "game", - "Item": { - "Title": "tetris", - "Price": 5 - } - }, - { - "Type": "toy", - "Item": { - "Title": "yoda", - "Price": 150 - } - } -] diff --git a/interfaces/_old/12-reflection/game.go b/interfaces/_old/12-reflection/game.go deleted file mode 100644 index 3604b15..0000000 --- a/interfaces/_old/12-reflection/game.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type game struct { - product -} diff --git a/interfaces/_old/12-reflection/list.go b/interfaces/_old/12-reflection/list.go deleted file mode 100644 index 01b6eaa..0000000 --- a/interfaces/_old/12-reflection/list.go +++ /dev/null @@ -1,40 +0,0 @@ -// 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" - "strings" -) - -type item interface { - discount(ratio float64) - fmt.Stringer -} - -type list []item - -func (l list) String() string { - if len(l) == 0 { - return "Sorry. We're waiting for delivery 🚚." - } - - var str strings.Builder - for _, it := range l { - str.WriteString(it.String()) - str.WriteRune('\n') - } - - return str.String() -} - -func (l list) discount(ratio float64) { - for _, it := range l { - it.discount(ratio) - } -} diff --git a/interfaces/_old/12-reflection/main.go b/interfaces/_old/12-reflection/main.go deleted file mode 100644 index 9a7e1d7..0000000 --- a/interfaces/_old/12-reflection/main.go +++ /dev/null @@ -1,55 +0,0 @@ -// 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 ( - "encoding/json" - "fmt" - "log" -) - -func main() { - /* - data, err := ioutil.ReadFile("database.json") - if err != nil { - log.Fatalln(err) - } - - var store list - - db := database{list: &store} - - if err := json.Unmarshal(data, &db); err != nil { - log.Fatalln(err) - } - - fmt.Print(store) - */ - - store := list{ - &book{product{"moby dick", 10}, toTimestamp(118281600)}, - &book{product{"odyssey", 15}, toTimestamp("733622400")}, - &book{product{"hobbit", 25}, unknown}, - &puzzle{product{"rubik's cube", 5}}, - &game{product{"minecraft", 20}}, - &game{product{"tetris", 5}}, - &toy{product{"yoda", 150}}, - } - - db := database{list: &store} - - out, err := json.MarshalIndent(&db, "", "\t") - if err != nil { - log.Fatalln(err) - } - - fmt.Println(string(out)) - - // store.discount(.5) - // fmt.Print(store) -} diff --git a/interfaces/_old/12-reflection/money.go b/interfaces/_old/12-reflection/money.go deleted file mode 100644 index 095a039..0000000 --- a/interfaces/_old/12-reflection/money.go +++ /dev/null @@ -1,16 +0,0 @@ -// 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) -} diff --git a/interfaces/_old/12-reflection/product.go b/interfaces/_old/12-reflection/product.go deleted file mode 100644 index f93cb84..0000000 --- a/interfaces/_old/12-reflection/product.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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 product struct { - Title string - Price money -} - -func (p *product) String() string { - return fmt.Sprintf("%-15s: %s", p.Title, p.Price) -} - -func (p *product) discount(ratio float64) { - p.Price *= money(1 - ratio) -} diff --git a/interfaces/_old/12-reflection/puzzle.go b/interfaces/_old/12-reflection/puzzle.go deleted file mode 100644 index acbaa47..0000000 --- a/interfaces/_old/12-reflection/puzzle.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type puzzle struct { - product -} diff --git a/interfaces/_old/12-reflection/timestamp.go b/interfaces/_old/12-reflection/timestamp.go deleted file mode 100644 index 04732e6..0000000 --- a/interfaces/_old/12-reflection/timestamp.go +++ /dev/null @@ -1,64 +0,0 @@ -// 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 ( - "strconv" - "time" -) - -type timestamp struct { - time.Time -} - -// timestamp knows how to decode itself from json. -// -// UnmarshalJSON is an implementation of the json.Unmarshaler interface. -// json.Unmarshal and json.Decode call this method. -func (ts *timestamp) UnmarshalJSON(data []byte) error { - *ts = toTimestamp(string(data)) - return nil -} - -// timestamp knows how to encode itself to json. -// -// MarshalJSON is an implementation of the json.Marshaler interface. -// json.Marshal and json.Encode call this method. -func (ts timestamp) MarshalJSON() (out []byte, err error) { - return strconv.AppendInt(out, ts.Unix(), 10), nil -} - -func (ts timestamp) String() string { - if ts.IsZero() { - return "unknown" - } - - // Mon Jan 2 15:04:05 -0700 MST 2006 - const layout = "2006/01" - return ts.Format(layout) -} - -func toTimestamp(v interface{}) timestamp { - var t int - - switch v := v.(type) { - case int: - // book{title: "moby dick", price: 10, published: 118281600}, - t = v - case string: - // book{title: "odyssey", price: 15, published: "733622400"}, - t, _ = strconv.Atoi(v) - default: - // book{title: "hobbit", price: 25}, - return timestamp{} - } - - return timestamp{ - Time: time.Unix(int64(t), 0), - } -} diff --git a/interfaces/_old/12-reflection/toy.go b/interfaces/_old/12-reflection/toy.go deleted file mode 100644 index ab5613d..0000000 --- a/interfaces/_old/12-reflection/toy.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type toy struct { - product -} diff --git a/interfaces/_old/13-reflection-2/book.go b/interfaces/_old/13-reflection-2/book.go deleted file mode 100644 index c36372f..0000000 --- a/interfaces/_old/13-reflection-2/book.go +++ /dev/null @@ -1,21 +0,0 @@ -// 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 { - product - Published timestamp -} - -func (b *book) String() string { - return fmt.Sprintf("%s - (%s)", &b.product, b.Published) -} diff --git a/interfaces/_old/13-reflection-2/database.go b/interfaces/_old/13-reflection-2/database.go deleted file mode 100644 index 5177deb..0000000 --- a/interfaces/_old/13-reflection-2/database.go +++ /dev/null @@ -1,88 +0,0 @@ -// 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 ( - "encoding/json" - "fmt" - "reflect" -) - -type database struct { - list *list - types map[string]item // the registry of the types -} - -func (db *database) MarshalJSON() ([]byte, error) { - type encodable struct { - Type string - Item item - } - - var e []encodable - - for _, it := range *db.list { - // TypeOf -> finds the dynamic type of "it" - // Elem -> returns the element type of the pointer - // Name -> returns the type name as string - t := reflect.TypeOf(it).Elem().Name() - e = append(e, encodable{t, it}) - } - - return json.Marshal(e) -} - -func (db *database) UnmarshalJSON(data []byte) error { - var decodables []struct { - Type string - Item json.RawMessage - } - - if err := json.Unmarshal(data, &decodables); err != nil { - return err - } - - for _, d := range decodables { - it, err := db.newItem(d.Item, d.Type) - if err != nil { - return err - } - - *db.list = append(*db.list, it) - } - - return nil -} - -func (db *database) newItem(data []byte, typ string) (item, error) { - it, ok := db.types[typ] - if !ok { - return nil, fmt.Errorf("newItem: type (%q) does not exist", typ) - } - - // get the dynamic type inside "it" like *book, *game, etc.. - t := reflect.TypeOf(it) - - // get the pointer type's element type like book, game, etc... - e := t.Elem() - - // create a new pointer value of the type like new(book), new(game), etc... - v := reflect.New(e) - - // get the interface value and cast it to the item type - it = v.Interface().(item) - - return it, json.Unmarshal(data, &it) -} - -func (db *database) register(typ string, it item) { - if db.types == nil { - db.types = make(map[string]item) - } - db.types[typ] = it -} diff --git a/interfaces/_old/13-reflection-2/database.json b/interfaces/_old/13-reflection-2/database.json deleted file mode 100644 index cccab62..0000000 --- a/interfaces/_old/13-reflection-2/database.json +++ /dev/null @@ -1,54 +0,0 @@ -[ - { - "Type": "book", - "Item": { - "Title": "moby dick", - "Price": 10, - "Published": 118281600 - } - }, - { - "Type": "book", - "Item": { - "Title": "odyssey", - "Price": 15, - "Published": 733622400 - } - }, - { - "Type": "book", - "Item": { - "Title": "hobbit", - "Price": 25, - "Published": -62135596800 - } - }, - { - "Type": "puzzle", - "Item": { - "Title": "rubik's cube", - "Price": 5 - } - }, - { - "Type": "game", - "Item": { - "Title": "minecraft", - "Price": 20 - } - }, - { - "Type": "game", - "Item": { - "Title": "tetris", - "Price": 5 - } - }, - { - "Type": "toy", - "Item": { - "Title": "yoda", - "Price": 150 - } - } -] diff --git a/interfaces/_old/13-reflection-2/game.go b/interfaces/_old/13-reflection-2/game.go deleted file mode 100644 index 3604b15..0000000 --- a/interfaces/_old/13-reflection-2/game.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type game struct { - product -} diff --git a/interfaces/_old/13-reflection-2/list.go b/interfaces/_old/13-reflection-2/list.go deleted file mode 100644 index 01b6eaa..0000000 --- a/interfaces/_old/13-reflection-2/list.go +++ /dev/null @@ -1,40 +0,0 @@ -// 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" - "strings" -) - -type item interface { - discount(ratio float64) - fmt.Stringer -} - -type list []item - -func (l list) String() string { - if len(l) == 0 { - return "Sorry. We're waiting for delivery 🚚." - } - - var str strings.Builder - for _, it := range l { - str.WriteString(it.String()) - str.WriteRune('\n') - } - - return str.String() -} - -func (l list) discount(ratio float64) { - for _, it := range l { - it.discount(ratio) - } -} diff --git a/interfaces/_old/13-reflection-2/main.go b/interfaces/_old/13-reflection-2/main.go deleted file mode 100644 index 70ebdfc..0000000 --- a/interfaces/_old/13-reflection-2/main.go +++ /dev/null @@ -1,60 +0,0 @@ -// 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 ( - "encoding/json" - "fmt" - "io/ioutil" - "log" -) - -func main() { - data, err := ioutil.ReadFile("database.json") - if err != nil { - log.Fatalln(err) - } - - var store list - - db := database{list: &store} - db.register("book", new(book)) - db.register("game", new(game)) - db.register("puzzle", new(puzzle)) - db.register("toy", new(toy)) - - if err := json.Unmarshal(data, &db); err != nil { - log.Fatalln(err) - } - - fmt.Print(store) - - /* - store := list{ - &book{product{"moby dick", 10}, toTimestamp(118281600)}, - &book{product{"odyssey", 15}, toTimestamp("733622400")}, - &book{product{"hobbit", 25}, unknown}, - &puzzle{product{"rubik's cube", 5}}, - &game{product{"minecraft", 20}}, - &game{product{"tetris", 5}}, - &toy{product{"yoda", 150}}, - } - - db := database{list: &store} - - out, err := json.MarshalIndent(&db, "", "\t") - if err != nil { - log.Fatalln(err) - } - - fmt.Println(string(out)) - - // store.discount(.5) - // fmt.Print(store) - */ -} diff --git a/interfaces/_old/13-reflection-2/money.go b/interfaces/_old/13-reflection-2/money.go deleted file mode 100644 index 095a039..0000000 --- a/interfaces/_old/13-reflection-2/money.go +++ /dev/null @@ -1,16 +0,0 @@ -// 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) -} diff --git a/interfaces/_old/13-reflection-2/product.go b/interfaces/_old/13-reflection-2/product.go deleted file mode 100644 index f93cb84..0000000 --- a/interfaces/_old/13-reflection-2/product.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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 product struct { - Title string - Price money -} - -func (p *product) String() string { - return fmt.Sprintf("%-15s: %s", p.Title, p.Price) -} - -func (p *product) discount(ratio float64) { - p.Price *= money(1 - ratio) -} diff --git a/interfaces/_old/13-reflection-2/puzzle.go b/interfaces/_old/13-reflection-2/puzzle.go deleted file mode 100644 index acbaa47..0000000 --- a/interfaces/_old/13-reflection-2/puzzle.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type puzzle struct { - product -} diff --git a/interfaces/_old/13-reflection-2/timestamp.go b/interfaces/_old/13-reflection-2/timestamp.go deleted file mode 100644 index 04732e6..0000000 --- a/interfaces/_old/13-reflection-2/timestamp.go +++ /dev/null @@ -1,64 +0,0 @@ -// 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 ( - "strconv" - "time" -) - -type timestamp struct { - time.Time -} - -// timestamp knows how to decode itself from json. -// -// UnmarshalJSON is an implementation of the json.Unmarshaler interface. -// json.Unmarshal and json.Decode call this method. -func (ts *timestamp) UnmarshalJSON(data []byte) error { - *ts = toTimestamp(string(data)) - return nil -} - -// timestamp knows how to encode itself to json. -// -// MarshalJSON is an implementation of the json.Marshaler interface. -// json.Marshal and json.Encode call this method. -func (ts timestamp) MarshalJSON() (out []byte, err error) { - return strconv.AppendInt(out, ts.Unix(), 10), nil -} - -func (ts timestamp) String() string { - if ts.IsZero() { - return "unknown" - } - - // Mon Jan 2 15:04:05 -0700 MST 2006 - const layout = "2006/01" - return ts.Format(layout) -} - -func toTimestamp(v interface{}) timestamp { - var t int - - switch v := v.(type) { - case int: - // book{title: "moby dick", price: 10, published: 118281600}, - t = v - case string: - // book{title: "odyssey", price: 15, published: "733622400"}, - t, _ = strconv.Atoi(v) - default: - // book{title: "hobbit", price: 25}, - return timestamp{} - } - - return timestamp{ - Time: time.Unix(int64(t), 0), - } -} diff --git a/interfaces/_old/13-reflection-2/toy.go b/interfaces/_old/13-reflection-2/toy.go deleted file mode 100644 index ab5613d..0000000 --- a/interfaces/_old/13-reflection-2/toy.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type toy struct { - product -} diff --git a/interfaces/_old/14-io-reader/book.go b/interfaces/_old/14-io-reader/book.go deleted file mode 100644 index c36372f..0000000 --- a/interfaces/_old/14-io-reader/book.go +++ /dev/null @@ -1,21 +0,0 @@ -// 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 { - product - Published timestamp -} - -func (b *book) String() string { - return fmt.Sprintf("%s - (%s)", &b.product, b.Published) -} diff --git a/interfaces/_old/14-io-reader/database.go b/interfaces/_old/14-io-reader/database.go deleted file mode 100644 index 9c0dc30..0000000 --- a/interfaces/_old/14-io-reader/database.go +++ /dev/null @@ -1,91 +0,0 @@ -// 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 ( - "encoding/json" - "fmt" - "io" - "io/ioutil" - "reflect" -) - -type database struct { - list *list - types map[string]item // the registry of the types -} - -// load the list by decoding the data from any data source. -func (db *database) load(r io.Reader) error { - // ReadAll reads from `r` entirely into memory. - // `data` contains the entire data in memory. - data, err := ioutil.ReadAll(r) - if err != nil { - return err - } - return json.Unmarshal(data, db) -} - -func (db *database) MarshalJSON() ([]byte, error) { - type encodable struct { - Type string - Item item - } - - var e []encodable - - for _, it := range *db.list { - t := reflect.TypeOf(it).Elem().Name() - e = append(e, encodable{t, it}) - } - - return json.Marshal(e) -} - -func (db *database) UnmarshalJSON(data []byte) error { - var decodables []struct { - Type string - Item json.RawMessage - } - - if err := json.Unmarshal(data, &decodables); err != nil { - return err - } - - for _, d := range decodables { - it, err := db.newItem(d.Item, d.Type) - if err != nil { - return err - } - - *db.list = append(*db.list, it) - } - - return nil -} - -func (db *database) newItem(data []byte, typ string) (item, error) { - it, ok := db.types[typ] - if !ok { - return nil, fmt.Errorf("newItem: type (%q) does not exist", typ) - } - - t := reflect.TypeOf(it) - e := t.Elem() - v := reflect.New(e) - it = v.Interface().(item) - - return it, json.Unmarshal(data, &it) -} - -func (db *database) register(typ string, it item) { - if db.types == nil { - db.types = make(map[string]item) - } - db.types[typ] = it -} diff --git a/interfaces/_old/14-io-reader/database.json b/interfaces/_old/14-io-reader/database.json deleted file mode 100644 index 5c9516c..0000000 --- a/interfaces/_old/14-io-reader/database.json +++ /dev/null @@ -1,54 +0,0 @@ -[ - { - "Type": "book", - "Item": { - "Title": "moby dick", - "Price": 10, - "Published": 118281600 - } - }, - { - "Type": "book", - "Item": { - "Title": "odyssey", - "Price": 15, - "Published": 733622400 - } - }, - { - "Type": "book", - "Item": { - "Title": "hobbit", - "Price": 25, - "Published": -62135596800 - } - }, - { - "Type": "puzzle", - "Item": { - "Title": "rubik's cube", - "Price": 5 - } - }, - { - "Type": "game", - "Item": { - "Title": "minecraft", - "Price": 20 - } - }, - { - "Type": "game", - "Item": { - "Title": "tetris", - "Price": 5 - } - }, - { - "Type": "toy", - "Item": { - "Title": "yoda", - "Price": 150 - } - } -] \ No newline at end of file diff --git a/interfaces/_old/14-io-reader/game.go b/interfaces/_old/14-io-reader/game.go deleted file mode 100644 index 3604b15..0000000 --- a/interfaces/_old/14-io-reader/game.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type game struct { - product -} diff --git a/interfaces/_old/14-io-reader/list.go b/interfaces/_old/14-io-reader/list.go deleted file mode 100644 index 01b6eaa..0000000 --- a/interfaces/_old/14-io-reader/list.go +++ /dev/null @@ -1,40 +0,0 @@ -// 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" - "strings" -) - -type item interface { - discount(ratio float64) - fmt.Stringer -} - -type list []item - -func (l list) String() string { - if len(l) == 0 { - return "Sorry. We're waiting for delivery 🚚." - } - - var str strings.Builder - for _, it := range l { - str.WriteString(it.String()) - str.WriteRune('\n') - } - - return str.String() -} - -func (l list) discount(ratio float64) { - for _, it := range l { - it.discount(ratio) - } -} diff --git a/interfaces/_old/14-io-reader/main.go b/interfaces/_old/14-io-reader/main.go deleted file mode 100644 index 4d8244a..0000000 --- a/interfaces/_old/14-io-reader/main.go +++ /dev/null @@ -1,66 +0,0 @@ -// 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" - "net/http" -) - -func main() { - /* - store := list{ - &book{product{"moby dick", 10}, toTimestamp(118281600)}, - &book{product{"odyssey", 15}, toTimestamp("733622400")}, - &book{product{"hobbit", 25}, unknown}, - &puzzle{product{"rubik's cube", 5}}, - &game{product{"minecraft", 20}}, - &game{product{"tetris", 5}}, - &toy{product{"yoda", 150}}, - } - - db := database{list: &store} - - out, err := json.MarshalIndent(&db, "", "\t") - if err != nil { - log.Fatalln(err) - } - - fmt.Println(string(out)) - - // store.discount(.5) - // fmt.Print(store) - */ - - var store list - - db := database{list: &store} - db.register("book", new(book)) - db.register("game", new(game)) - db.register("puzzle", new(puzzle)) - db.register("toy", new(toy)) - - // load from a file - // f, _ := os.Open("database.json") - // db.load(f) - // f.Close() - - // load from a string - // const data = `[ - // { "Type": "book", "Item": { "Title": "1984", "Price": 8, "Published": -649641600 } }, - // { "Type": "game", "Item": { "Title": "paperboy", "Price": 20 } }]` - // r := strings.NewReader(data) - // db.load(r) - - // load from a web server - res, _ := http.Get("https://inancgumus.github.io/x/database.json") - db.load(res.Body) - res.Body.Close() - - fmt.Print(store) -} diff --git a/interfaces/_old/14-io-reader/money.go b/interfaces/_old/14-io-reader/money.go deleted file mode 100644 index 095a039..0000000 --- a/interfaces/_old/14-io-reader/money.go +++ /dev/null @@ -1,16 +0,0 @@ -// 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) -} diff --git a/interfaces/_old/14-io-reader/product.go b/interfaces/_old/14-io-reader/product.go deleted file mode 100644 index f93cb84..0000000 --- a/interfaces/_old/14-io-reader/product.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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 product struct { - Title string - Price money -} - -func (p *product) String() string { - return fmt.Sprintf("%-15s: %s", p.Title, p.Price) -} - -func (p *product) discount(ratio float64) { - p.Price *= money(1 - ratio) -} diff --git a/interfaces/_old/14-io-reader/puzzle.go b/interfaces/_old/14-io-reader/puzzle.go deleted file mode 100644 index acbaa47..0000000 --- a/interfaces/_old/14-io-reader/puzzle.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type puzzle struct { - product -} diff --git a/interfaces/_old/14-io-reader/timestamp.go b/interfaces/_old/14-io-reader/timestamp.go deleted file mode 100644 index 04732e6..0000000 --- a/interfaces/_old/14-io-reader/timestamp.go +++ /dev/null @@ -1,64 +0,0 @@ -// 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 ( - "strconv" - "time" -) - -type timestamp struct { - time.Time -} - -// timestamp knows how to decode itself from json. -// -// UnmarshalJSON is an implementation of the json.Unmarshaler interface. -// json.Unmarshal and json.Decode call this method. -func (ts *timestamp) UnmarshalJSON(data []byte) error { - *ts = toTimestamp(string(data)) - return nil -} - -// timestamp knows how to encode itself to json. -// -// MarshalJSON is an implementation of the json.Marshaler interface. -// json.Marshal and json.Encode call this method. -func (ts timestamp) MarshalJSON() (out []byte, err error) { - return strconv.AppendInt(out, ts.Unix(), 10), nil -} - -func (ts timestamp) String() string { - if ts.IsZero() { - return "unknown" - } - - // Mon Jan 2 15:04:05 -0700 MST 2006 - const layout = "2006/01" - return ts.Format(layout) -} - -func toTimestamp(v interface{}) timestamp { - var t int - - switch v := v.(type) { - case int: - // book{title: "moby dick", price: 10, published: 118281600}, - t = v - case string: - // book{title: "odyssey", price: 15, published: "733622400"}, - t, _ = strconv.Atoi(v) - default: - // book{title: "hobbit", price: 25}, - return timestamp{} - } - - return timestamp{ - Time: time.Unix(int64(t), 0), - } -} diff --git a/interfaces/_old/14-io-reader/toy.go b/interfaces/_old/14-io-reader/toy.go deleted file mode 100644 index ab5613d..0000000 --- a/interfaces/_old/14-io-reader/toy.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type toy struct { - product -} diff --git a/interfaces/_old/15-io-reader-composition/book.go b/interfaces/_old/15-io-reader-composition/book.go deleted file mode 100644 index c36372f..0000000 --- a/interfaces/_old/15-io-reader-composition/book.go +++ /dev/null @@ -1,21 +0,0 @@ -// 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 { - product - Published timestamp -} - -func (b *book) String() string { - return fmt.Sprintf("%s - (%s)", &b.product, b.Published) -} diff --git a/interfaces/_old/15-io-reader-composition/counter.go b/interfaces/_old/15-io-reader-composition/counter.go deleted file mode 100644 index ebe327a..0000000 --- a/interfaces/_old/15-io-reader-composition/counter.go +++ /dev/null @@ -1,35 +0,0 @@ -// 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 "io" - -// counter counts the total number of bytes read. -type counter struct { - r io.Reader - n int -} - -// newCounter wraps `r` and returns a *counter. -func newCounter(r io.Reader) *counter { - return &counter{r: r} -} - -// Total bytes read. -func (c *counter) total() int { - return c.n -} - -// Read counts the number of bytes read from the underlying reader. -func (c *counter) Read(p []byte) (n int, err error) { - n, err = c.r.Read(p) // read from the underlying reader - if n > 0 { - c.n += n // sum the number of bytes read with the total bytes counted - } - return n, err // return the number of bytes read and an error -} diff --git a/interfaces/_old/15-io-reader-composition/database.go b/interfaces/_old/15-io-reader-composition/database.go deleted file mode 100644 index 5b65d6a..0000000 --- a/interfaces/_old/15-io-reader-composition/database.go +++ /dev/null @@ -1,112 +0,0 @@ -// 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 ( - "encoding/json" - "fmt" - "io" - "reflect" -) - -type database struct { - list *list - types map[string]item -} - -// load the list by decoding the data from any kind of data source. -func (db *database) load(r io.Reader) error { - return json.NewDecoder(r).Decode(db) -} - -func (db *database) MarshalJSON() ([]byte, error) { - type encodable struct { - Type string - Item item - } - - var e []encodable - - for _, it := range *db.list { - t := reflect.TypeOf(it).Elem().Name() - e = append(e, encodable{t, it}) - } - - return json.Marshal(e) -} - -func (db *database) UnmarshalJSON(data []byte) error { - var decodables []struct { - Type string - Item json.RawMessage - } - - if err := json.Unmarshal(data, &decodables); err != nil { - return err - } - - for _, d := range decodables { - it, err := db.newItem(d.Item, d.Type) - if err != nil { - return err - } - - *db.list = append(*db.list, it) - } - - return nil -} - -func (db *database) newItem(data []byte, typ string) (item, error) { - it, ok := db.types[typ] - if !ok { - return nil, fmt.Errorf("newItem: type (%q) does not exist", typ) - } - - t := reflect.TypeOf(it) - e := t.Elem() - v := reflect.New(e) - it = v.Interface().(item) - - return it, json.Unmarshal(data, &it) -} - -func (db *database) register(typ string, it item) { - if db.types == nil { - db.types = make(map[string]item) - } - db.types[typ] = it -} - -// I put it here as a reference implementation of manually reading from a reader. -// func (db *database) loadX(r io.Reader) error { -// buf := make([]byte, 64) -// -// var ( -// data []byte -// n int -// err error -// ) -// -// for { -// n, err = r.Read(buf) -// -// if n > 0 { -// data = append(data, buf[:n]...) -// } -// -// if err != nil { -// break -// } -// } -// -// if err != io.EOF { -// return err -// } -// return json.Unmarshal(data, db) -// } diff --git a/interfaces/_old/15-io-reader-composition/database.json b/interfaces/_old/15-io-reader-composition/database.json deleted file mode 100644 index 5c9516c..0000000 --- a/interfaces/_old/15-io-reader-composition/database.json +++ /dev/null @@ -1,54 +0,0 @@ -[ - { - "Type": "book", - "Item": { - "Title": "moby dick", - "Price": 10, - "Published": 118281600 - } - }, - { - "Type": "book", - "Item": { - "Title": "odyssey", - "Price": 15, - "Published": 733622400 - } - }, - { - "Type": "book", - "Item": { - "Title": "hobbit", - "Price": 25, - "Published": -62135596800 - } - }, - { - "Type": "puzzle", - "Item": { - "Title": "rubik's cube", - "Price": 5 - } - }, - { - "Type": "game", - "Item": { - "Title": "minecraft", - "Price": 20 - } - }, - { - "Type": "game", - "Item": { - "Title": "tetris", - "Price": 5 - } - }, - { - "Type": "toy", - "Item": { - "Title": "yoda", - "Price": 150 - } - } -] \ No newline at end of file diff --git a/interfaces/_old/15-io-reader-composition/database.json.gz b/interfaces/_old/15-io-reader-composition/database.json.gz deleted file mode 100644 index fca1d73..0000000 Binary files a/interfaces/_old/15-io-reader-composition/database.json.gz and /dev/null differ diff --git a/interfaces/_old/15-io-reader-composition/game.go b/interfaces/_old/15-io-reader-composition/game.go deleted file mode 100644 index 3604b15..0000000 --- a/interfaces/_old/15-io-reader-composition/game.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type game struct { - product -} diff --git a/interfaces/_old/15-io-reader-composition/list.go b/interfaces/_old/15-io-reader-composition/list.go deleted file mode 100644 index 01b6eaa..0000000 --- a/interfaces/_old/15-io-reader-composition/list.go +++ /dev/null @@ -1,40 +0,0 @@ -// 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" - "strings" -) - -type item interface { - discount(ratio float64) - fmt.Stringer -} - -type list []item - -func (l list) String() string { - if len(l) == 0 { - return "Sorry. We're waiting for delivery 🚚." - } - - var str strings.Builder - for _, it := range l { - str.WriteString(it.String()) - str.WriteRune('\n') - } - - return str.String() -} - -func (l list) discount(ratio float64) { - for _, it := range l { - it.discount(ratio) - } -} diff --git a/interfaces/_old/15-io-reader-composition/main.go b/interfaces/_old/15-io-reader-composition/main.go deleted file mode 100644 index 35b6412..0000000 --- a/interfaces/_old/15-io-reader-composition/main.go +++ /dev/null @@ -1,96 +0,0 @@ -// 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 ( - "compress/gzip" - "fmt" - "net/http" -) - -func main() { - /* - store := list{ - &book{product{"moby dick", 10}, toTimestamp(118281600)}, - &book{product{"odyssey", 15}, toTimestamp("733622400")}, - &book{product{"hobbit", 25}, unknown}, - &puzzle{product{"rubik's cube", 5}}, - &game{product{"minecraft", 20}}, - &game{product{"tetris", 5}}, - &toy{product{"yoda", 150}}, - } - - db := database{list: &store} - - out, err := json.MarshalIndent(&db, "", "\t") - if err != nil { - log.Fatalln(err) - } - - fmt.Println(string(out)) - - // store.discount(.5) - // fmt.Print(store) - */ - - var store list - - db := database{list: &store} - db.register("book", new(book)) - db.register("game", new(game)) - db.register("puzzle", new(puzzle)) - db.register("toy", new(toy)) - - // load from a file - // f, _ := os.Open("database.json") - // db.load(f) - // f.Close() - - // load from a string - // const data = `[ - // { "Type": "book", "Item": { "Title": "1984", "Price": 8, "Published": -649641600 } }, - // { "Type": "game", "Item": { "Title": "paperboy", "Price": 20 } }]` - // r := strings.NewReader(data) - // db.load(r) - - // load from a web server - // res, _ := http.Get("https://inancgumus.github.io/x/database.json") - // db.load(res.Body) - // res.Body.Close() - - // decompress the reader while reading from it - // res, _ := http.Get("https://inancgumus.github.io/x/database.json.gz") - // gr, _ := gzip.NewReader(res.Body) - // db.load(gr) - // gr.Close() - // res.Body.Close() - - // decompress and count the number of compressed bytes read - // res, _ := http.Get("https://inancgumus.github.io/x/database.json.gz") - // c := newCounter(res.Body) // count the bytes read - // gr, _ := gzip.NewReader(c) - - // db.load(gr) - // fmt.Printf("%d bytes read.\n\n", c.total()) - - // gr.Close() - // res.Body.Close() - - // count the number of compressed bytes read from the web server then decompress - res, _ := http.Get("https://inancgumus.github.io/x/database.json.gz") - gr, _ := gzip.NewReader(res.Body) - c := newCounter(gr) - - db.load(c) - fmt.Printf("%d bytes read.\n\n", c.total()) - - gr.Close() - res.Body.Close() - - fmt.Print(store) -} diff --git a/interfaces/_old/15-io-reader-composition/money.go b/interfaces/_old/15-io-reader-composition/money.go deleted file mode 100644 index 095a039..0000000 --- a/interfaces/_old/15-io-reader-composition/money.go +++ /dev/null @@ -1,16 +0,0 @@ -// 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) -} diff --git a/interfaces/_old/15-io-reader-composition/product.go b/interfaces/_old/15-io-reader-composition/product.go deleted file mode 100644 index f93cb84..0000000 --- a/interfaces/_old/15-io-reader-composition/product.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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 product struct { - Title string - Price money -} - -func (p *product) String() string { - return fmt.Sprintf("%-15s: %s", p.Title, p.Price) -} - -func (p *product) discount(ratio float64) { - p.Price *= money(1 - ratio) -} diff --git a/interfaces/_old/15-io-reader-composition/puzzle.go b/interfaces/_old/15-io-reader-composition/puzzle.go deleted file mode 100644 index acbaa47..0000000 --- a/interfaces/_old/15-io-reader-composition/puzzle.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type puzzle struct { - product -} diff --git a/interfaces/_old/15-io-reader-composition/timestamp.go b/interfaces/_old/15-io-reader-composition/timestamp.go deleted file mode 100644 index 04732e6..0000000 --- a/interfaces/_old/15-io-reader-composition/timestamp.go +++ /dev/null @@ -1,64 +0,0 @@ -// 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 ( - "strconv" - "time" -) - -type timestamp struct { - time.Time -} - -// timestamp knows how to decode itself from json. -// -// UnmarshalJSON is an implementation of the json.Unmarshaler interface. -// json.Unmarshal and json.Decode call this method. -func (ts *timestamp) UnmarshalJSON(data []byte) error { - *ts = toTimestamp(string(data)) - return nil -} - -// timestamp knows how to encode itself to json. -// -// MarshalJSON is an implementation of the json.Marshaler interface. -// json.Marshal and json.Encode call this method. -func (ts timestamp) MarshalJSON() (out []byte, err error) { - return strconv.AppendInt(out, ts.Unix(), 10), nil -} - -func (ts timestamp) String() string { - if ts.IsZero() { - return "unknown" - } - - // Mon Jan 2 15:04:05 -0700 MST 2006 - const layout = "2006/01" - return ts.Format(layout) -} - -func toTimestamp(v interface{}) timestamp { - var t int - - switch v := v.(type) { - case int: - // book{title: "moby dick", price: 10, published: 118281600}, - t = v - case string: - // book{title: "odyssey", price: 15, published: "733622400"}, - t, _ = strconv.Atoi(v) - default: - // book{title: "hobbit", price: 25}, - return timestamp{} - } - - return timestamp{ - Time: time.Unix(int64(t), 0), - } -} diff --git a/interfaces/_old/15-io-reader-composition/toy.go b/interfaces/_old/15-io-reader-composition/toy.go deleted file mode 100644 index ab5613d..0000000 --- a/interfaces/_old/15-io-reader-composition/toy.go +++ /dev/null @@ -1,12 +0,0 @@ -// 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 - -type toy struct { - product -}