2019-09-07 23:04:53 +03:00
|
|
|
// 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"
|
2019-09-09 13:29:55 +03:00
|
|
|
"net/http"
|
2019-09-07 23:04:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
/*
|
|
|
|
store := list{
|
|
|
|
&book{product{"moby dick", 10}, toTimestamp(118281600)},
|
|
|
|
&book{product{"odyssey", 15}, toTimestamp("733622400")},
|
|
|
|
&book{product{"hobbit", 25}, unknown},
|
|
|
|
&puzzle{product{"rubik's cube", 5}},
|
|
|
|
&game{product{"minecraft", 20}},
|
|
|
|
&game{product{"tetris", 5}},
|
|
|
|
&toy{product{"yoda", 150}},
|
|
|
|
}
|
|
|
|
|
|
|
|
db := database{list: &store}
|
|
|
|
|
|
|
|
out, err := json.MarshalIndent(&db, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(out))
|
|
|
|
|
|
|
|
// store.discount(.5)
|
|
|
|
// fmt.Print(store)
|
|
|
|
*/
|
2019-09-09 13:29:55 +03:00
|
|
|
|
|
|
|
var store list
|
|
|
|
|
|
|
|
db := database{list: &store}
|
|
|
|
db.register("book", new(book))
|
|
|
|
db.register("book", new(book))
|
|
|
|
db.register("game", new(game))
|
|
|
|
db.register("puzzle", new(puzzle))
|
|
|
|
db.register("toy", new(toy))
|
|
|
|
|
|
|
|
// load from a file
|
|
|
|
// f, _ := os.Open("database.json")
|
|
|
|
// db.load(f)
|
|
|
|
// f.Close()
|
|
|
|
|
|
|
|
// load from a string
|
|
|
|
// const data = `[
|
|
|
|
// { "Type": "book", "Item": { "Title": "1984", "Price": 8, "Published": -649641600 } },
|
|
|
|
// { "Type": "game", "Item": { "Title": "paperboy", "Price": 20 } }]`
|
|
|
|
// r := strings.NewReader(data)
|
|
|
|
// db.load(r)
|
|
|
|
|
|
|
|
// load from a web server
|
|
|
|
res, _ := http.Get("https://inancgumus.github.io/x/database.json")
|
|
|
|
db.load(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
|
|
|
|
fmt.Print(store)
|
2019-09-07 23:04:53 +03:00
|
|
|
}
|