refactor: common ifaces
This commit is contained in:
17
interfaces/13-io/database.json
Normal file
17
interfaces/13-io/database.json
Normal file
@ -0,0 +1,17 @@
|
||||
[
|
||||
{
|
||||
"title": "moby dick",
|
||||
"price": 10,
|
||||
"released": 118281600
|
||||
},
|
||||
{
|
||||
"title": "odyssey",
|
||||
"price": 15,
|
||||
"released": 733622400
|
||||
},
|
||||
{
|
||||
"title": "hobbit",
|
||||
"price": 25,
|
||||
"released": -62135596800
|
||||
}
|
||||
]
|
59
interfaces/13-io/json.go
Normal file
59
interfaces/13-io/json.go
Normal file
@ -0,0 +1,59 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
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, err error) {
|
||||
if err := json.Unmarshal(data, &l); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func decodeFile(path string) (list, error) {
|
||||
// ReadAll reads entire bytes from a file to memory.
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decode(data)
|
||||
}
|
||||
|
||||
func decodeFileObject(f *os.File) (list, error) {
|
||||
data, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decode(data)
|
||||
}
|
||||
|
||||
func decodeReader(r io.Reader) (l list, err error) {
|
||||
// data, err := ioutil.ReadAll(r)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return decode(data)
|
||||
|
||||
return l, json.NewDecoder(r).Decode(&l)
|
||||
}
|
49
interfaces/13-io/list.go
Normal file
49
interfaces/13-io/list.go
Normal file
@ -0,0 +1,49 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type list []*product
|
||||
|
||||
func (l list) String() string {
|
||||
if len(l) == 0 {
|
||||
return "Sorry. We're waiting for delivery 🚚.\n"
|
||||
}
|
||||
|
||||
sort.Sort(l)
|
||||
var str strings.Builder
|
||||
for _, p := range l {
|
||||
fmt.Fprintf(&str, "* %s\n", p)
|
||||
}
|
||||
return str.String()
|
||||
}
|
||||
|
||||
func (l list) discount(ratio float64) {
|
||||
for _, p := range l {
|
||||
p.discount(ratio)
|
||||
}
|
||||
}
|
||||
|
||||
// by default `list` sorts by `Title`.
|
||||
func (l list) Len() int { return len(l) }
|
||||
func (l list) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
||||
func (l list) Less(i, j int) bool { return l[i].Title < l[j].Title }
|
||||
|
||||
// byRelease sorts by product release dates.
|
||||
type byRelease struct {
|
||||
list
|
||||
}
|
||||
|
||||
func (bp byRelease) Less(i, j int) bool {
|
||||
return bp.list[i].Released.Before(bp.list[j].Released.Time)
|
||||
}
|
47
interfaces/13-io/main.go
Normal file
47
interfaces/13-io/main.go
Normal file
@ -0,0 +1,47 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// removed error handling for brevity (normally you shouldn't do this).
|
||||
|
||||
// encode to memory (to a byte slice)
|
||||
// out, _ := encode()
|
||||
// fmt.Println(string(out))
|
||||
|
||||
// read from memory (from a byte slice):
|
||||
// l, _ := decode(out)
|
||||
|
||||
// read from a file by name:
|
||||
// l, _ := decodeFile("database.json")
|
||||
|
||||
// read from a file #1 (not good):
|
||||
// f, _ := os.Open("database.json")
|
||||
// l, _ := decodeFileObject(f)
|
||||
// f.Close()
|
||||
|
||||
// read from a file #2 (better):
|
||||
// f, _ := os.Open("database.json")
|
||||
// l, _ := decodeReader(f)
|
||||
// f.Close()
|
||||
|
||||
// read from memory (from a string):
|
||||
// const data = `[
|
||||
// { "title": "moby dick", "price": 10, "released": 118281600 },
|
||||
// { "title": "odyssey", "price": 15, "released": 733622400 },
|
||||
// { "title": "hobbit", "price": 25, "released": -62135596800 }
|
||||
// ]`
|
||||
// l, _ := decodeReader(strings.NewReader(data))
|
||||
|
||||
// read from inanc's web server:
|
||||
// res, _ := http.Get("https://inancgumus.github.io/x/database.json")
|
||||
// l, _ := decodeReader(res.Body)
|
||||
// res.Body.Close()
|
||||
|
||||
// fmt.Print(l)
|
||||
}
|
16
interfaces/13-io/money.go
Normal file
16
interfaces/13-io/money.go
Normal file
@ -0,0 +1,16 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type money float64
|
||||
|
||||
func (m money) String() string {
|
||||
return fmt.Sprintf("$%.2f", m)
|
||||
}
|
26
interfaces/13-io/product.go
Normal file
26
interfaces/13-io/product.go
Normal file
@ -0,0 +1,26 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type product struct {
|
||||
Title string `json:"title"`
|
||||
Price money `json:"price"`
|
||||
Released timestamp `json:"released,omitempty"`
|
||||
}
|
||||
|
||||
func (p *product) String() string {
|
||||
return fmt.Sprintf("%s: %s (%s)", p.Title, p.Price, p.Released)
|
||||
}
|
||||
|
||||
func (p *product) discount(ratio float64) {
|
||||
p.Price *= money(1 - ratio)
|
||||
}
|
50
interfaces/13-io/timestamp.go
Normal file
50
interfaces/13-io/timestamp.go
Normal file
@ -0,0 +1,50 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type timestamp struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (ts timestamp) String() string {
|
||||
if ts.IsZero() {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// Mon Jan 2 15:04:05 -0700 MST 2006
|
||||
const layout = "2006/01"
|
||||
return ts.Format(layout)
|
||||
}
|
||||
|
||||
func (ts *timestamp) UnmarshalJSON(data []byte) error {
|
||||
*ts = toTimestamp(string(data))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts timestamp) MarshalJSON() (out []byte, err error) {
|
||||
return strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||
}
|
||||
|
||||
func toTimestamp(v interface{}) (ts timestamp) {
|
||||
var t int
|
||||
|
||||
switch v := v.(type) {
|
||||
case int:
|
||||
t = v
|
||||
case string:
|
||||
t, _ = strconv.Atoi(v)
|
||||
}
|
||||
|
||||
ts.Time = time.Unix(int64(t), 0)
|
||||
return ts
|
||||
}
|
Reference in New Issue
Block a user