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() + // } +}