From 7c91b2a882066ad4623cd3372ece9cb85fdf048d Mon Sep 17 00:00:00 2001 From: hoang Date: Sat, 6 Jun 2020 16:29:25 +0700 Subject: [PATCH] c2-m3 --- c2-m3/animal.go | 46 ++++++++++++++-------------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/c2-m3/animal.go b/c2-m3/animal.go index faa0dd0..991ad7a 100644 --- a/c2-m3/animal.go +++ b/c2-m3/animal.go @@ -27,43 +27,25 @@ func (animal *Animal) Speak() { } func main() { - cow := Animal{food: "grass", locomotion: "walk", noise: "moo"} - bird := Animal{"worms", "fly", "peep"} - snake := Animal{"mice", "slither", "hsss"} + animals := map[string]Animal{ + "cow": {food: "grass", locomotion: "walk", noise: "moo"}, + "bird": {"worms", "fly", "peep"}, + "snake": {"mice", "slither", "hsss"}, + } + for { var s1, s2 string fmt.Println("Please input request as 2 strings seperate by a space") fmt.Print(">") fmt.Scan(&s1, &s2) - switch s1 { - case "cow": - switch s2 { - case "eat": - cow.Eat() - case "move": - cow.Move() - case "speak": - cow.Speak() - } - case "bird": - switch s2 { - case "eat": - bird.Eat() - case "move": - bird.Move() - case "speak": - bird.Speak() - } - case "snake": - switch s2 { - case "eat": - snake.Eat() - case "move": - snake.Move() - case "speak": - snake.Speak() - } - + animal := animals[s1] + switch s2 { + case "eat": + animal.Eat() + case "move": + animal.Move() + case "speak": + animal.Speak() } } }