fix: match the code to the videos
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user