Browse Source

Add examples & exercise 7.02 of chapter 07

T. Meissner 1 month ago
parent
commit
731e51b53d

+ 4
- 0
chapter_07/example_1/data.json View File

@@ -0,0 +1,4 @@
1
+{
2
+    "Name": "Torsten",
3
+    "Age": 41
4
+}

+ 65
- 0
chapter_07/example_1/main.go View File

@@ -0,0 +1,65 @@
1
+package main
2
+
3
+import (
4
+  "encoding/json"
5
+  "fmt"
6
+  "io"
7
+  "os"
8
+  "strings"
9
+)
10
+
11
+type Person struct {
12
+  Name string `json:"name"`
13
+  Age  int    `json:"age"`
14
+}
15
+
16
+func loadPerson2(s string) (Person, error) {
17
+
18
+  var p Person
19
+  err := json.NewDecoder(strings.NewReader(s)).Decode(&p)
20
+  if err != nil {
21
+    return p, err
22
+  }
23
+  return p, nil
24
+
25
+}
26
+
27
+func loadPerson(r io.Reader) (Person, error) {
28
+
29
+  var p Person
30
+  err := json.NewDecoder(r).Decode(&p)
31
+  if err != nil {
32
+    return p, err
33
+  }
34
+  return p, nil
35
+
36
+}
37
+
38
+func main() {
39
+
40
+  s := `{"Name":"Joe","Age":18}`
41
+  s2 := `{"Name":"Jane","Age":21}`
42
+
43
+  p, err := loadPerson(strings.NewReader(s))
44
+  if err != nil {
45
+    fmt.Println(err)
46
+  }
47
+  fmt.Println(p)
48
+
49
+  p2, err := loadPerson2(s2)
50
+  if err != nil {
51
+    fmt.Println(err)
52
+  }
53
+  fmt.Println(p2)
54
+
55
+  f, err := os.Open("data.json")
56
+  if err != nil {
57
+    fmt.Println(err)
58
+  }
59
+  p3, err := loadPerson(f)
60
+  if err != nil {
61
+    fmt.Println(err)
62
+  }
63
+  fmt.Println(p3)
64
+
65
+}

+ 33
- 0
chapter_07/example_2/main.go View File

@@ -0,0 +1,33 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+type Speaker interface {
6
+  Speak() string
7
+}
8
+
9
+type cat struct {
10
+  name string
11
+}
12
+
13
+func catDetails(c cat) {
14
+  fmt.Printf("(%v, %T)\n", c, c)
15
+}
16
+
17
+func emptyDetails(s interface{}) {
18
+  fmt.Printf("(%v, %T)\n", s, s)
19
+}
20
+
21
+func main() {
22
+
23
+  c := cat{"oreo"}
24
+  i := 99
25
+  b := false
26
+  str := "test"
27
+  catDetails(c)
28
+  emptyDetails(c)
29
+  emptyDetails(i)
30
+  emptyDetails(b)
31
+  emptyDetails(str)
32
+
33
+}

+ 31
- 0
chapter_07/example_3/main.go View File

@@ -0,0 +1,31 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+type cat struct {
6
+  name string
7
+}
8
+
9
+func typeExample(i []interface{}) {
10
+
11
+  for _, x := range i {
12
+    switch v := x.(type) {
13
+    case int:
14
+      fmt.Printf("%v is an int\n", v)
15
+    case string:
16
+      fmt.Printf("%v is a string\n", v)
17
+    case bool:
18
+      fmt.Printf("%v is a boolean\n", v)
19
+    default:
20
+      fmt.Printf("Unknown type %T\n", v)
21
+    }
22
+  }
23
+}
24
+
25
+func main() {
26
+
27
+  c := cat{"oreo"}
28
+  i := []interface{}{42, "The book club", true, c}
29
+  typeExample(i)
30
+
31
+}

+ 72
- 0
chapter_07/exercise_7.03/main.go View File

@@ -0,0 +1,72 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+type record struct {
6
+  key       string
7
+  valueType string
8
+  data      interface{}
9
+}
10
+
11
+type person struct {
12
+  lastName  string
13
+  age       int
14
+  isMarried bool
15
+}
16
+
17
+type animal struct {
18
+  name     string
19
+  category string
20
+}
21
+
22
+func newRecord(key string, i interface{}) record {
23
+
24
+  r := record{}
25
+  r.key = key
26
+
27
+  switch v := i.(type) {
28
+  case int:
29
+    r.valueType = "int"
30
+    r.data = v
31
+  case bool:
32
+    r.valueType = "bool"
33
+    r.data = v
34
+  case string:
35
+    r.valueType = "string"
36
+    r.data = v
37
+  case person:
38
+    r.valueType = "person"
39
+    r.data = v
40
+  default:
41
+    r.valueType = "unknown"
42
+    r.data = v
43
+  }
44
+  return r
45
+
46
+}
47
+
48
+func main() {
49
+
50
+  m := make(map[string]interface{})
51
+  a := animal{name: "oreo", category: "cat"}
52
+  p := person{lastName: "Doe", isMarried: false, age: 19}
53
+  m["person"] = p
54
+  m["animal"] = a
55
+  m["age"] = 54
56
+  m["isMarried"] = true
57
+  m["lastName"] = "Smith"
58
+
59
+  rs := []record{}
60
+  for k, v := range m {
61
+    r := newRecord(k, v)
62
+    rs = append(rs, r)
63
+  }
64
+
65
+  for _, v := range rs {
66
+    fmt.Println("Key:", v.key)
67
+    fmt.Println("Data:", v.data)
68
+    fmt.Println("Type:", v.valueType)
69
+    fmt.Println()
70
+  }
71
+
72
+}