forked from akashpayneSRO/gotraining-studyguide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion_2.go
84 lines (69 loc) · 2.44 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
// -----------------------
// 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 compile time.
if v, ok := mvs[rn].(cloud); ok {
fmt.Println("Got Lucky:", v)
continue
}
// We have to guarantee that variable in question (x in `x.(T)`) can always be asserted correctly as T type
// Or else, We wouldn't want to use that ok variable because we want it to panic if there is an integrity
// issue. We must shut it down immediately if that happens if we cannot recover from a
// panic and guarantee that we are back at 100% integrity, the software has to be restarted.
// Shutting down means you have to call log.Fatal, os.exit, or panic for stack trace.
// When we use type assertion, we need to understand when it is okay that whatever
// we are asking for is not there.
// Important note:
// ---------------
// If the type assertion is causing us to call the concrete value out, that should raise a big
// flag. We are using interface to maintain a level of decoupling and now we are using type
// assertion 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")
}
}