forked from joselitofilho/aulas-golang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution_1_main.go
60 lines (47 loc) · 992 Bytes
/
solution_1_main.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
package main
import "fmt"
type Key interface {
Press() interface{}
}
type Keyboard struct {
LastKeyPressed interface{}
Keys []Key
}
func (keyboard *Keyboard) PressAllKeys() {
for _, key := range keyboard.Keys {
keyboard.LastKeyPressed = key.Press()
}
}
type KeyHelloWorld struct {
key string
}
func NewKeyHelloWorld() KeyHelloWorld {
return KeyHelloWorld{"Hello World"}
}
func (khw KeyHelloWorld) Press() interface{} {
return khw.key
}
type Key100 struct{}
func (k Key100) Press() interface{} {
return 100
}
func main() {
// keyboard := Keyboard{
// Keys: []Key{
// NewKeyHelloWorld(),
// Key100{},
// },
// }
keyboard := Keyboard{}
keyboard.Keys = append(keyboard.Keys, Key100{}, NewKeyHelloWorld())
keyboard.PressAllKeys()
fmt.Println(keyboard.LastKeyPressed, keyboard.Keys)
switch v := keyboard.LastKeyPressed.(type) {
case int:
fmt.Println("int", v)
case string:
fmt.Println("string", v)
default:
fmt.Println("default", v)
}
}