fix: match the code to the videos

This commit is contained in:
Inanc Gumus 2019-12-09 20:40:03 +03:00
parent be7bc962f7
commit b8fec94208
4 changed files with 52 additions and 56 deletions

View File

@ -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)

View File

@ -1,17 +1,17 @@
[
{
"title": "moby dick",
"price": 10,
"released": 118281600
},
{
"title": "odyssey",
"price": 15,
"released": 733622400
},
{
"title": "hobbit",
"price": 25,
"released": -62135596800
}
]
{
"title": "moby dick",
"price": 10,
"released": 118281600
},
{
"title": "odyssey",
"price": 15,
"released": 733622400
},
{
"title": "hobbit",
"price": 25,
"released": -62135596800
}
]

View File

@ -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
}

View File

@ -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)
}
/*