You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
636 B
Go
42 lines
636 B
Go
1 year ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
type Animal struct {
|
||
|
food, locomotion, sound string
|
||
|
}
|
||
|
|
||
|
func (v Animal) Eat() {
|
||
|
fmt.Println(v.food)
|
||
|
}
|
||
|
|
||
|
func (v Animal) Move() {
|
||
|
fmt.Println(v.locomotion)
|
||
|
}
|
||
|
|
||
|
func (v Animal) Speak() {
|
||
|
fmt.Println(v.sound)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
m := map[string]Animal{
|
||
|
"cow" : Animal{"grass","walk","moo"},
|
||
|
"bird" : Animal{"worms","fly","peep"},
|
||
|
"snake" : Animal{"mice","slither","hsss"},
|
||
|
}
|
||
|
for{
|
||
|
fmt.Print(">")
|
||
|
an:="0"
|
||
|
ac:="0"
|
||
|
fmt.Scan(&an,&ac)
|
||
|
if ac=="eat"{
|
||
|
m[an].Eat()
|
||
|
} else if ac=="move"{
|
||
|
m[an].Move()
|
||
|
} else if ac=="speak"{
|
||
|
m[an].Speak()
|
||
|
}
|
||
|
}
|
||
|
}
|