Files
learngo/interfaces/12-marshaler/json.go

28 lines
732 B
Go
Raw Normal View History

2019-10-17 14:11:51 +03:00
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
2019-10-30 19:34:44 +03:00
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
2019-10-17 14:11:51 +03:00
package main
2019-10-21 15:21:34 +03:00
import "encoding/json"
2019-10-17 14:11:51 +03:00
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")
}
2019-10-21 15:21:34 +03:00
func decode(data []byte) (l list, _ error) {
2019-10-17 14:11:51 +03:00
if err := json.Unmarshal(data, &l); err != nil {
return nil, err
}
2019-10-21 15:21:34 +03:00
return
2019-10-17 14:11:51 +03:00
}