forked from hoanhan101/ultimate-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversion_2.go
86 lines (72 loc) · 2.57 KB
/
conversion_2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// -----------------------
// Runtime Type Assertions
// -----------------------
package main
import (
"fmt"
"math/rand"
"time"
)
// car represents something you drive.
type car struct{}
// String implements the fmt.Stringer interface.
func (car) String() string {
return "Vroom!"
}
// cloud represents somewhere you store information.
type cloud struct{}
// String implements the fmt.Stringer interface.
func (cloud) String() string {
return "Big Data!"
}
func main() {
// Seed the number random generator.
rand.Seed(time.Now().UnixNano())
// Create a slice of the Stringer interface values.
// ---------------------
// | car | cloud |
// ---------------------
// | * | * |
// ---------------------
// A A
// | |
// car cloud
// ----- -----
// | | | |
// ----- -----
mvs := []fmt.Stringer{
car{},
cloud{},
}
// Let's run this experiment ten times.
for i := 0; i < 10; i++ {
// Choose a random number from 0 to 1.
rn := rand.Intn(2)
// Perform a type assertion that we have a concrete type of cloud in the interface
// value we randomly chose.
// This shows us that this checking is at runtime, not compiled time.
if v, ok := mvs[rn].(cloud); ok {
fmt.Println("Got Lucky:", v)
continue
}
// This shows us that this checking is at runtime, not compiled time. We have to decide
// if there should always be a value of some type that should never change.
// We wouldn't want to use that ok because we want it to panic if there is a integrity
// issue. We must shut it down immediately if that happen. If we cannot recover from a
// panic and give the guarantee that you are back at 100% integrity, the software has to be
// restarted. Shutting downs means you have to either call log.Fatal or os.exit, or panic
// for stack trace.
// When we use type assertion, we need to understand where or not it is okay that whatever
// we are asking for is not there.
// Important note:
// ---------------
// When we are using type assertion, it raises a big red flag.
// If the type assertion is causing us to call the concrete value out, that should raise a
// flag. We are using interface to maintain a level of decoupling and now we are using type
// asserting to go back to the concrete.
// When we are in the concrete, we are putting our codes in the situation where cascading
// changes can cause widespread refactoring. What we want with interface is the opposite,
// internal changes minimize cascading changes.
fmt.Println("Got Unlucky")
}
}