Add interfaces exercise
This commit is contained in:
parent
baa3b77552
commit
f240e98da4
|
@ -0,0 +1,3 @@
|
|||
module interfaces
|
||||
|
||||
go 1.22.1
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue