-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimal2.go
60 lines (53 loc) · 1.08 KB
/
animal2.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 animal struct {
food string
locomotion string
noise string
}
type animalInterface interface {
Eat()
Move()
Speak()
}
func (ani animal) Eat() {
fmt.Println(ani.food)
return
}
func (ani animal) Move() {
fmt.Println(ani.locomotion)
return
}
func (ani animal) Speak() {
fmt.Println(ani.noise)
return
}
func main() {
animalMap := make(map[string]animal)
animalMap["cow"] = animal{"grass", "walk", "moo"}
animalMap["bird"] = animal{"worms", "fly", "peep"}
animalMap["snake"] = animal{"mice", "slither", "hsss"}
var genralAni animalInterface
for {
var command, requestAni, requestType string
fmt.Print(">")
fmt.Scan(&command, &requestAni, &requestType)
if command == "query" {
genralAni = animalMap[requestAni]
switch requestType {
case "eat":
genralAni.Eat()
case "move":
genralAni.Move()
case "speak":
genralAni.Speak()
}
}
if command == "newanimal" {
animalMap[requestAni] = animalMap[requestType]
fmt.Println("Created it!")
}
}
}