Add interfaces exercise

This commit is contained in:
raul 2024-04-16 08:14:52 +02:00
parent baa3b77552
commit f240e98da4
2 changed files with 42 additions and 0 deletions

View File

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

View File

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