interfaces

This commit is contained in:
Benjamyn Love 2021-10-02 16:28:28 +10:00
parent 7a51de11e4
commit 7bb1fe712b
4 changed files with 57 additions and 0 deletions

3
interfaces/go.mod Normal file
View File

@ -0,0 +1,3 @@
module lovelynet.net/interfaces
go 1.17

32
interfaces/main.go Normal file
View File

@ -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!"
}

3
maps/go.mod Normal file
View File

@ -0,0 +1,3 @@
module lovelynet.net/maps
go 1.17

19
maps/main.go Normal file
View File

@ -0,0 +1,19 @@
package main
import "fmt"
func main() {
colours := map[string]string{
"red": "#ff0000",
"green": "#4bf745",
"white": "#ffffff",
}
printMap(colours)
}
func printMap(c map[string]string) {
for colour, hex := range c {
fmt.Println(colour, hex)
}
}