animal data output from coursera

PHOTO EMBED

Tue Nov 15 2022 16:51:18 GMT+0000 (UTC)

Saved by @Plastikov #go

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

// Write a program which allows the user to get information about a predefined set of animals; "cow", "bird" and "snake"
// Your program should present the user with a prompt “>”
// Your program accepts one request at a time from the user, prints out the answer to the request, and prints out a new prompt
// Your program should continue in this loop forever
// Every request from the user must be a single line containing 2 strings, the former an animal and the latter an action, e.g "cow" "speak"
// Your program should process each request by printing out the requested data

type Animal struct {
	food       string
	locomotion string
	noise      string
}

func (a Animal) Eat() string {
	return a.food
}

func (a Animal) Move() string {
	return a.locomotion
}

func (a Animal) Speak() string {
	return a.noise
}

func main() {
	cow := Animal{food: "grass", locomotion: "walk", noise: "moo"}
	bird := Animal{food: "seeds", locomotion: "fly", noise: "peep"}
	snake := Animal{food: "mouse", locomotion: "slither", noise: "hsss"}

	animalMap := make(map[string]Animal)
	animalMap["cow"] = cow
	animalMap["bird"] = bird
	animalMap["snake"] = snake

	scanner := bufio.NewScanner(os.Stdin)
	for {
		fmt.Println(">")
		scanner.Scan()
		inpt := strings.Split(scanner.Text(), " ")

		animal := animalMap[inpt[0]]

		switch {
		case inpt[1] == "speak":
			fmt.Println(animal.Speak())
		case inpt[1] == "eat":
			fmt.Println(animal.Eat())
		case inpt[1] == "move":
			fmt.Println(animal.Move())
		}
	}
}
content_copyCOPY

this is the first animal program