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