Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "bufio"
- "strings"
- "os"
- )
- type Animals struct {
- eat string
- move string
- speak string
- }
- func (animal *Animals) Eat() {
- fmt.Printf(animal.eat)
- }
- func (animal *Animals) Move() {
- fmt.Printf(animal.move)
- }
- func (animal *Animals) Speak() {
- fmt.Printf(animal.speak)
- }
- func callFunc (an Animals, in string) () {
- switch in {
- case "eat":
- an.Eat()
- break
- case "move":
- an.Move()
- break
- case "speak":
- an.Speak()
- break
- }
- }
- func main() {
- var in []string
- var command string
- var an2 Animals
- input := bufio.NewScanner(os.Stdin)
- cow := Animals{"grass", "walk", "moo"}
- bird := Animals{"worms", "fly", "peep"}
- snake := Animals{"mice", "slither", "hsss"}
- dic2 := map[string]Animals {
- "cow" : cow,
- "bird" : bird,
- "snake" : snake,
- }
- fmt.Printf(">")
- for input.Scan() {
- command = input.Text()
- in = strings.Split(command, " ")
- an2 = dic2[in[0]]
- callFunc(an2, in[1])
- fmt.Printf("\n>")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement