From f240e98da426ce688d958975b9b4d1f8b5dfc17f Mon Sep 17 00:00:00 2001 From: raul Date: Tue, 16 Apr 2024 08:14:52 +0200 Subject: [PATCH] Add interfaces exercise --- tour-of-go/interfaces/go.mod | 3 +++ tour-of-go/interfaces/main.go | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 tour-of-go/interfaces/go.mod create mode 100644 tour-of-go/interfaces/main.go diff --git a/tour-of-go/interfaces/go.mod b/tour-of-go/interfaces/go.mod new file mode 100644 index 0000000..fd215e1 --- /dev/null +++ b/tour-of-go/interfaces/go.mod @@ -0,0 +1,3 @@ +module interfaces + +go 1.22.1 diff --git a/tour-of-go/interfaces/main.go b/tour-of-go/interfaces/main.go new file mode 100644 index 0000000..25cf81f --- /dev/null +++ b/tour-of-go/interfaces/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" +) + +type Reader interface { + Read() +} + +type Dog struct { + name string + sound string +} + +type Cat struct { + name string + sound string +} + +func callRead(r Reader) { + r.Read() +} + +func (d Dog) Read() { + fmt.Printf("%v goes %v\n", d.name, d.sound) +} + +func (c Cat) Read() { + fmt.Printf("%v goes %v\n", c.name, c.sound) +} + +func main() { + d := Dog{"Max", "Woof"} + c := Cat{"Mimi", "Meow"} + + callRead(d) + callRead(c) +}