massive: move a lot of things
This commit is contained in:
38
x-tba/wizards-structs/marshal/main.go
Normal file
38
x-tba/wizards-structs/marshal/main.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// 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"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Wizard is one of the greatest of people
|
||||
type Wizard struct {
|
||||
// name won't be marshalled (should be exported)
|
||||
Name string `json:"name,omitempty"`
|
||||
Lastname string `json:"last_name"`
|
||||
Nick string `json:"-"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
wizards := []Wizard{
|
||||
{Name: "Albert", Lastname: "Einstein", Nick: "emc2"},
|
||||
{Name: "Isaac", Lastname: "Newton", Nick: "apple"},
|
||||
{Name: "Stephen", Lastname: "Hawking", Nick: "blackhole"},
|
||||
{Name: "Marie", Lastname: "Curie", Nick: "radium"},
|
||||
{Name: "", Lastname: "Darwin", Nick: "fittest"},
|
||||
}
|
||||
|
||||
bytes, err := json.MarshalIndent(wizards, "", "\t")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(bytes))
|
||||
}
|
5
x-tba/wizards-structs/server/list.tmpl.html
Normal file
5
x-tba/wizards-structs/server/list.tmpl.html
Normal file
@@ -0,0 +1,5 @@
|
||||
<h1>There are {{len .}} wizards.</h1>
|
||||
|
||||
{{range .}}
|
||||
👉 {{.Name}} {{.Lastname}} ({{.Nick}})
|
||||
{{end}}
|
46
x-tba/wizards-structs/server/main.go
Normal file
46
x-tba/wizards-structs/server/main.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/add", onlyPost(add))
|
||||
http.HandleFunc("/list", list)
|
||||
panic(http.ListenAndServe(":8000", nil))
|
||||
}
|
||||
|
||||
func add(w http.ResponseWriter, r *http.Request) {
|
||||
var wiz wizard
|
||||
if err := json.NewDecoder(r.Body).Decode(&wiz); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
db.add(wiz)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func list(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
tmpl.Execute(w, db.list())
|
||||
}
|
||||
|
||||
func onlyPost(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
next(w, r)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("NOT OK"))
|
||||
}
|
||||
}
|
32
x-tba/wizards-structs/server/store.go
Normal file
32
x-tba/wizards-structs/server/store.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import "sync"
|
||||
|
||||
type wizard struct {
|
||||
Name string `json:"name"`
|
||||
Lastname string `json:"last_name"`
|
||||
Nick string `json:"nick"`
|
||||
}
|
||||
|
||||
type storage struct {
|
||||
sync.RWMutex
|
||||
wizards []wizard
|
||||
}
|
||||
|
||||
func (db *storage) add(w wizard) {
|
||||
db.Lock()
|
||||
defer db.Unlock()
|
||||
db.wizards = append(db.wizards, w)
|
||||
}
|
||||
|
||||
func (db *storage) list() []wizard {
|
||||
db.RLock()
|
||||
defer db.RUnlock()
|
||||
return db.wizards
|
||||
}
|
||||
|
||||
var db *storage
|
||||
|
||||
func init() {
|
||||
db = new(storage)
|
||||
}
|
11
x-tba/wizards-structs/server/templates.go
Normal file
11
x-tba/wizards-structs/server/templates.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import "html/template"
|
||||
|
||||
var tmpl *template.Template
|
||||
|
||||
func init() {
|
||||
tmpl = template.Must(
|
||||
template.New("list.tmpl").
|
||||
ParseFiles("list.tmpl"))
|
||||
}
|
35
x-tba/wizards-structs/unmarshal/main.go
Normal file
35
x-tba/wizards-structs/unmarshal/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Wizard is one of the greatest of people
|
||||
type Wizard struct {
|
||||
// name won't be marshalled (should be exported)
|
||||
Name string `json:name`
|
||||
Lastname string `json:"-"`
|
||||
Nick string `json:"nick"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
file, err := ioutil.ReadFile("../marshal/wizards.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
wizards := make([]Wizard, 10)
|
||||
if json.Unmarshal(file, &wizards) != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("%-15s %-15s\n%s",
|
||||
"Name", "Nick", strings.Repeat("=", 25))
|
||||
|
||||
for _, w := range wizards {
|
||||
fmt.Printf("%-15s %-15s\n", w.Name, w.Nick)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user