Interfaces
Examples in
V
// A type implements an interface by implementing its methods.
// There is no explicit declaration of intent, no "implements" keyword.
struct Dog {}
struct Cat {}
fn (d Dog) speak() string {
return 'woof'
}
fn (c Cat) speak() string {
return 'meow'
}
interface Speaker {
speak() string
}
fn perform(s Speaker) {
// TODO
str := s.speak()
println(str)
}
dog := Dog{}
cat := Cat{}
perform(dog) // "woof"
perform(cat) // "meow"
Last Run
:
woof
meow