Browse Source

Add remaining code of chapter 04

T. Meissner 1 month ago
parent
commit
e597f7fea6

+ 41
- 0
chapter_04/activity_4.06/main.go View File

@@ -0,0 +1,41 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func getData() []interface{} {
6
+
7
+  return []interface{}{
8
+    1,
9
+    3.14,
10
+    "hello",
11
+    true,
12
+    struct{}{},
13
+  }
14
+
15
+}
16
+
17
+func typeCheck(v interface{}) string {
18
+
19
+  switch v.(type) {
20
+  case string:
21
+    return "string"
22
+  case bool:
23
+    return "bool"
24
+  case float32, float64:
25
+    return "float"
26
+  case int, int32, int64:
27
+    return "int"
28
+  default:
29
+    return "unknown"
30
+  }
31
+
32
+}
33
+
34
+func main() {
35
+
36
+  data := getData()
37
+  for i := 0; i < len(data); i++ {
38
+    fmt.Printf("%v is %v\n", data[i], typeCheck(data[i]))
39
+  }
40
+
41
+}

+ 40
- 0
chapter_04/exercise_4.18/main.go View File

@@ -0,0 +1,40 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+type point struct {
6
+  x int
7
+  y int
8
+}
9
+
10
+func compare() (bool, bool) {
11
+
12
+  point1 := struct {
13
+    x int
14
+    y int
15
+  }{
16
+    10,
17
+    10,
18
+  }
19
+
20
+  point2 := struct {
21
+    x int
22
+    y int
23
+  }{}
24
+  point2.x = 10
25
+  point2.y = 5
26
+
27
+  point3 := point{10, 10}
28
+
29
+  return point1 == point2, point1 == point3
30
+
31
+}
32
+
33
+func main() {
34
+
35
+  a, b := compare()
36
+
37
+  fmt.Println("point1 == point2:", a)
38
+  fmt.Println("point1 == point3:", b)
39
+
40
+}

+ 65
- 0
chapter_04/exercise_4.19/main.go View File

@@ -0,0 +1,65 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+type name string
6
+
7
+type location struct {
8
+  x int
9
+  y int
10
+}
11
+
12
+type size struct {
13
+  width  int
14
+  height int
15
+}
16
+
17
+type dot struct {
18
+  name
19
+  location
20
+  size
21
+}
22
+
23
+func getDots() []dot {
24
+
25
+  var dot1 dot
26
+
27
+  dot2 := dot{}
28
+  dot2.name = "A"
29
+  dot2.x = 5
30
+  dot2.y = 6
31
+  dot2.width = 10
32
+  dot2.height = 20
33
+
34
+  dot3 := dot{
35
+    name: "B",
36
+    location: location{
37
+      x: 13,
38
+      y: 27,
39
+    },
40
+    size: size{
41
+      width:  5,
42
+      height: 7,
43
+    },
44
+  }
45
+
46
+  dot4 := dot{}
47
+  dot4.name = "C"
48
+  dot4.x = 101
49
+  dot4.location.y = 209
50
+  dot4.width = 87
51
+  dot4.size.height = 43
52
+
53
+  return []dot{dot1, dot2, dot3, dot4}
54
+
55
+}
56
+
57
+func main() {
58
+
59
+  dots := getDots()
60
+
61
+  for i := 0; i < len(dots); i++ {
62
+    fmt.Printf("dot%v: %#v\n", i+1, dots[i])
63
+  }
64
+
65
+}

+ 26
- 0
chapter_04/exercise_4.20/main.go View File

@@ -0,0 +1,26 @@
1
+package main
2
+
3
+import (
4
+  "fmt"
5
+  "math"
6
+)
7
+
8
+func convert() string {
9
+
10
+  var i8 int8 = math.MaxInt8
11
+  i := 128
12
+  f64 := 3.14
13
+  m := fmt.Sprintf("int8    =  %v -> int64   =  %v\n", i8, int64(i8))
14
+  m += fmt.Sprintf("int     =  %v -> int8    = %v\n", i, int8(i))
15
+  m += fmt.Sprintf("int8    =  %v -> float32 =  %v\n", i8, float64(i8))
16
+  m += fmt.Sprintf("float64 = %v -> int     =    %v\n", f64, int(f64))
17
+
18
+  return m
19
+
20
+}
21
+
22
+func main() {
23
+
24
+  fmt.Print(convert())
25
+
26
+}

+ 29
- 0
chapter_04/exercise_4.21/main.go View File

@@ -0,0 +1,29 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+)
7
+
8
+func doubler(v interface{}) (string, error) {
9
+
10
+  if i, ok := v.(int); ok {
11
+    return fmt.Sprint(i * 2), nil
12
+  }
13
+  if s, ok := v.(string); ok {
14
+    return s + s, nil
15
+  }
16
+  return "", errors.New("unsupported type passed")
17
+
18
+}
19
+
20
+func main() {
21
+
22
+  res, _ := doubler(5)
23
+  fmt.Println("5 :", res)
24
+  res, _ = doubler("yum")
25
+  fmt.Println("yum:", res)
26
+  _, err := doubler(true)
27
+  fmt.Println("true:", err)
28
+
29
+}

+ 62
- 0
chapter_04/exercise_4.22/main.go View File

@@ -0,0 +1,62 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+)
7
+
8
+func doubler(v interface{}) (string, error) {
9
+
10
+  switch t := v.(type) {
11
+  case string:
12
+    return t + t, nil
13
+  case bool:
14
+    if t {
15
+      return "truetrue", nil
16
+    }
17
+    return "falsefalse", nil
18
+  case float32, float64:
19
+    if f, ok := t.(float64); ok {
20
+      return fmt.Sprint(f * 2), nil
21
+    }
22
+    return fmt.Sprint(t.(float32) * 2), nil
23
+  case int:
24
+    return fmt.Sprint(t * 2), nil
25
+  case int8:
26
+    return fmt.Sprint(t * 2), nil
27
+  case int16:
28
+    return fmt.Sprint(t * 2), nil
29
+  case int32:
30
+    return fmt.Sprint(t * 2), nil
31
+  case int64:
32
+    return fmt.Sprint(t * 2), nil
33
+  case uint:
34
+    return fmt.Sprint(t * 2), nil
35
+  case uint8:
36
+    return fmt.Sprint(t * 2), nil
37
+  case uint16:
38
+    return fmt.Sprint(t * 2), nil
39
+  case uint32:
40
+    return fmt.Sprint(t * 2), nil
41
+  case uint64:
42
+    return fmt.Sprint(t * 2), nil
43
+  default:
44
+    return "", errors.New("unsupported type passed")
45
+  }
46
+
47
+}
48
+
49
+func main() {
50
+
51
+  res, _ := doubler(-5)
52
+  fmt.Println("-5:  ", res)
53
+  res, _ = doubler(5)
54
+  fmt.Println("5:   ", res)
55
+  res, _ = doubler("yum")
56
+  fmt.Println("yum: ", res)
57
+  res, _ = doubler(true)
58
+  fmt.Println("true:", res)
59
+  res, _ = doubler(float32(3.14))
60
+  fmt.Println("3.14:", res)
61
+
62
+}