@ -0,0 +1,4 @@ | |||
{ | |||
"Name": "Torsten", | |||
"Age": 41 | |||
} |
@ -0,0 +1,65 @@ | |||
package main | |||
import ( | |||
"encoding/json" | |||
"fmt" | |||
"io" | |||
"os" | |||
"strings" | |||
) | |||
type Person struct { | |||
Name string `json:"name"` | |||
Age int `json:"age"` | |||
} | |||
func loadPerson2(s string) (Person, error) { | |||
var p Person | |||
err := json.NewDecoder(strings.NewReader(s)).Decode(&p) | |||
if err != nil { | |||
return p, err | |||
} | |||
return p, nil | |||
} | |||
func loadPerson(r io.Reader) (Person, error) { | |||
var p Person | |||
err := json.NewDecoder(r).Decode(&p) | |||
if err != nil { | |||
return p, err | |||
} | |||
return p, nil | |||
} | |||
func main() { | |||
s := `{"Name":"Joe","Age":18}` | |||
s2 := `{"Name":"Jane","Age":21}` | |||
p, err := loadPerson(strings.NewReader(s)) | |||
if err != nil { | |||
fmt.Println(err) | |||
} | |||
fmt.Println(p) | |||
p2, err := loadPerson2(s2) | |||
if err != nil { | |||
fmt.Println(err) | |||
} | |||
fmt.Println(p2) | |||
f, err := os.Open("data.json") | |||
if err != nil { | |||
fmt.Println(err) | |||
} | |||
p3, err := loadPerson(f) | |||
if err != nil { | |||
fmt.Println(err) | |||
} | |||
fmt.Println(p3) | |||
} |
@ -0,0 +1,33 @@ | |||
package main | |||
import "fmt" | |||
type Speaker interface { | |||
Speak() string | |||
} | |||
type cat struct { | |||
name string | |||
} | |||
func catDetails(c cat) { | |||
fmt.Printf("(%v, %T)\n", c, c) | |||
} | |||
func emptyDetails(s interface{}) { | |||
fmt.Printf("(%v, %T)\n", s, s) | |||
} | |||
func main() { | |||
c := cat{"oreo"} | |||
i := 99 | |||
b := false | |||
str := "test" | |||
catDetails(c) | |||
emptyDetails(c) | |||
emptyDetails(i) | |||
emptyDetails(b) | |||
emptyDetails(str) | |||
} |
@ -0,0 +1,31 @@ | |||
package main | |||
import "fmt" | |||
type cat struct { | |||
name string | |||
} | |||
func typeExample(i []interface{}) { | |||
for _, x := range i { | |||
switch v := x.(type) { | |||
case int: | |||
fmt.Printf("%v is an int\n", v) | |||
case string: | |||
fmt.Printf("%v is a string\n", v) | |||
case bool: | |||
fmt.Printf("%v is a boolean\n", v) | |||
default: | |||
fmt.Printf("Unknown type %T\n", v) | |||
} | |||
} | |||
} | |||
func main() { | |||
c := cat{"oreo"} | |||
i := []interface{}{42, "The book club", true, c} | |||
typeExample(i) | |||
} |
@ -0,0 +1,72 @@ | |||
package main | |||
import "fmt" | |||
type record struct { | |||
key string | |||
valueType string | |||
data interface{} | |||
} | |||
type person struct { | |||
lastName string | |||
age int | |||
isMarried bool | |||
} | |||
type animal struct { | |||
name string | |||
category string | |||
} | |||
func newRecord(key string, i interface{}) record { | |||
r := record{} | |||
r.key = key | |||
switch v := i.(type) { | |||
case int: | |||
r.valueType = "int" | |||
r.data = v | |||
case bool: | |||
r.valueType = "bool" | |||
r.data = v | |||
case string: | |||
r.valueType = "string" | |||
r.data = v | |||
case person: | |||
r.valueType = "person" | |||
r.data = v | |||
default: | |||
r.valueType = "unknown" | |||
r.data = v | |||
} | |||
return r | |||
} | |||
func main() { | |||
m := make(map[string]interface{}) | |||
a := animal{name: "oreo", category: "cat"} | |||
p := person{lastName: "Doe", isMarried: false, age: 19} | |||
m["person"] = p | |||
m["animal"] = a | |||
m["age"] = 54 | |||
m["isMarried"] = true | |||
m["lastName"] = "Smith" | |||
rs := []record{} | |||
for k, v := range m { | |||
r := newRecord(k, v) | |||
rs = append(rs, r) | |||
} | |||
for _, v := range rs { | |||
fmt.Println("Key:", v.key) | |||
fmt.Println("Data:", v.data) | |||
fmt.Println("Type:", v.valueType) | |||
fmt.Println() | |||
} | |||
} |