Cleaner interface exercise implementations

This commit is contained in:
raul 2024-04-18 09:10:30 +02:00
parent 725071ded5
commit b8fe0a216b
2 changed files with 51 additions and 0 deletions

3
interfaces/go.mod Normal file
View File

@ -0,0 +1,3 @@
module interfaces
go 1.22.2

48
interfaces/main.go Normal file
View File

@ -0,0 +1,48 @@
package main
import (
"fmt"
)
type Sounder interface {
AnimalSound()
}
type Cat struct {
sound string
}
type Dog struct {
sound string
}
type JavaProgrammer struct {
sound string
}
func (c Cat) AnimalSound() {
fmt.Println(c.sound)
}
func (d Dog) AnimalSound() {
fmt.Println(d.sound)
}
func (j JavaProgrammer) AnimalSound() {
fmt.Println(j.sound)
}
func MakeSound(s Sounder) {
s.AnimalSound()
}
func main() {
animals := []Sounder{Cat{sound: "Meow"}, Dog{sound: "Woof"}, JavaProgrammer{sound: "*Unintelligible*"}}
MakeSound(animals[2])
MakeSound(animals[1])
MakeSound(animals[0])
// for _, v := range animals {
// v.AnimalSound()
// }
}