From b8fec94208197ec1b7ab0e9b6978fea405cc0d5c Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Mon, 9 Dec 2019 20:40:03 +0300 Subject: [PATCH] fix: match the code to the videos --- interfaces/11-sort/main.go | 6 ++-- interfaces/12-marshaler/database.json | 32 ++++++++++---------- interfaces/12-marshaler/json.go | 27 ----------------- interfaces/12-marshaler/main.go | 43 ++++++++++++++++++++------- 4 files changed, 52 insertions(+), 56 deletions(-) delete mode 100644 interfaces/12-marshaler/json.go diff --git a/interfaces/11-sort/main.go b/interfaces/11-sort/main.go index d775599..486aefc 100644 --- a/interfaces/11-sort/main.go +++ b/interfaces/11-sort/main.go @@ -20,9 +20,9 @@ func main() { {title: "hobbit", price: 25}, } - sort.Sort(l) - sort.Sort(sort.Reverse(l)) - sort.Sort(byReleaseDate(l)) + // sort.Sort(l) + // sort.Sort(sort.Reverse(l)) + // sort.Sort(byReleaseDate(l)) sort.Sort(sort.Reverse(byReleaseDate(l))) fmt.Print(l) diff --git a/interfaces/12-marshaler/database.json b/interfaces/12-marshaler/database.json index 8a3df19..efd4b66 100644 --- a/interfaces/12-marshaler/database.json +++ b/interfaces/12-marshaler/database.json @@ -1,17 +1,17 @@ [ - { - "title": "moby dick", - "price": 10, - "released": 118281600 - }, - { - "title": "odyssey", - "price": 15, - "released": 733622400 - }, - { - "title": "hobbit", - "price": 25, - "released": -62135596800 - } -] \ No newline at end of file + { + "title": "moby dick", + "price": 10, + "released": 118281600 + }, + { + "title": "odyssey", + "price": 15, + "released": 733622400 + }, + { + "title": "hobbit", + "price": 25, + "released": -62135596800 + } +] diff --git a/interfaces/12-marshaler/json.go b/interfaces/12-marshaler/json.go deleted file mode 100644 index 484eb96..0000000 --- a/interfaces/12-marshaler/json.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "encoding/json" - -func encode() ([]byte, error) { - l := list{ - {Title: "moby dick", Price: 10, Released: toTimestamp(118281600)}, - {Title: "odyssey", Price: 15, Released: toTimestamp("733622400")}, - {Title: "hobbit", Price: 25}, - } - return json.MarshalIndent(l, "", "\t") -} - -func decode(data []byte) (l list, _ error) { - if err := json.Unmarshal(data, &l); err != nil { - return nil, err - } - return -} diff --git a/interfaces/12-marshaler/main.go b/interfaces/12-marshaler/main.go index 1af8345..32b778e 100644 --- a/interfaces/12-marshaler/main.go +++ b/interfaces/12-marshaler/main.go @@ -9,26 +9,49 @@ package main import ( + "encoding/json" "fmt" "log" ) +const data = `[ + { + "title": "moby dick", + "price": 10, + "released": 118281600 + }, + { + "title": "odyssey", + "price": 15, + "released": 733622400 + }, + { + "title": "hobbit", + "price": 25, + "released": -62135596800 + } +]` + func main() { - // First encode products as JSON: - data, err := encode() + /* encoding */ + l := list{ + {Title: "moby dick", Price: 10, Released: toTimestamp(118281600)}, + {Title: "odyssey", Price: 15, Released: toTimestamp("733622400")}, + {Title: "hobbit", Price: 25}, + } + + data, err := json.MarshalIndent(l, "", " ") if err != nil { log.Fatal(err) } fmt.Println(string(data)) - // Then decode them back from JSON: - l, err := decode(data) - if err != nil { - log.Fatal(err) - } - - // Let the list value print itself: - fmt.Print(l) + /* decoding */ + // var l list + // if err := json.Unmarshal([]byte(data), &l); err != nil { + // log.Fatal(err) + // } + // fmt.Print(l) } /*