interfaces
This commit is contained in:
1 parent
7a51de11e4
commit
7bb1fe712b
4 files changed
+57
No files matched your search
@@ -0,0 +1,3 @@
|
||||
module lovelynet.net/interfaces
|
||||
|
||||
go 1.17
|
||||
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type bot interface {
|
||||
getGreeting() string
|
||||
}
|
||||
|
||||
type englishBot struct{}
|
||||
type spanishBot struct{}
|
||||
|
||||
func main() {
|
||||
eb := englishBot{}
|
||||
sb := spanishBot{}
|
||||
|
||||
printGreetings(eb)
|
||||
printGreetings(sb)
|
||||
}
|
||||
|
||||
func printGreetings(b bot) {
|
||||
fmt.Println(b.getGreeting())
|
||||
}
|
||||
|
||||
func (englishBot) getGreeting() string {
|
||||
// Very custom logic
|
||||
return "Hello"
|
||||
}
|
||||
|
||||
func (spanishBot) getGreeting() string {
|
||||
// Very custom logic
|
||||
return "Hola!"
|
||||
}
|
||||
Reference in new issue
Block a user