From b8fe0a216b4cdac1b2270cbfbd9c1f8297fdc3a0 Mon Sep 17 00:00:00 2001 From: raul Date: Thu, 18 Apr 2024 09:10:30 +0200 Subject: [PATCH] Cleaner interface exercise implementations --- interfaces/go.mod | 3 +++ interfaces/main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 interfaces/go.mod create mode 100644 interfaces/main.go diff --git a/interfaces/go.mod b/interfaces/go.mod new file mode 100644 index 0000000..8b0f5be --- /dev/null +++ b/interfaces/go.mod @@ -0,0 +1,3 @@ +module interfaces + +go 1.22.2 diff --git a/interfaces/main.go b/interfaces/main.go new file mode 100644 index 0000000..45976f7 --- /dev/null +++ b/interfaces/main.go @@ -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() + // } +}