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

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