fix: match the code to the videos
This commit is contained in:
@ -20,9 +20,9 @@ func main() {
|
|||||||
{title: "hobbit", price: 25},
|
{title: "hobbit", price: 25},
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(l)
|
// sort.Sort(l)
|
||||||
sort.Sort(sort.Reverse(l))
|
// sort.Sort(sort.Reverse(l))
|
||||||
sort.Sort(byReleaseDate(l))
|
// sort.Sort(byReleaseDate(l))
|
||||||
sort.Sort(sort.Reverse(byReleaseDate(l)))
|
sort.Sort(sort.Reverse(byReleaseDate(l)))
|
||||||
|
|
||||||
fmt.Print(l)
|
fmt.Print(l)
|
||||||
|
@ -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
|
|
||||||
}
|
|
@ -9,26 +9,49 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const data = `[
|
||||||
|
{
|
||||||
|
"title": "moby dick",
|
||||||
|
"price": 10,
|
||||||
|
"released": 118281600
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "odyssey",
|
||||||
|
"price": 15,
|
||||||
|
"released": 733622400
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "hobbit",
|
||||||
|
"price": 25,
|
||||||
|
"released": -62135596800
|
||||||
|
}
|
||||||
|
]`
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// First encode products as JSON:
|
/* encoding */
|
||||||
data, err := encode()
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(data))
|
fmt.Println(string(data))
|
||||||
|
|
||||||
// Then decode them back from JSON:
|
/* decoding */
|
||||||
l, err := decode(data)
|
// var l list
|
||||||
if err != nil {
|
// if err := json.Unmarshal([]byte(data), &l); err != nil {
|
||||||
log.Fatal(err)
|
// log.Fatal(err)
|
||||||
}
|
// }
|
||||||
|
// fmt.Print(l)
|
||||||
// Let the list value print itself:
|
|
||||||
fmt.Print(l)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Reference in New Issue
Block a user