interfaces
This commit is contained in:
parent
7a51de11e4
commit
7bb1fe712b
3
interfaces/go.mod
Normal file
3
interfaces/go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module lovelynet.net/interfaces
|
||||||
|
|
||||||
|
go 1.17
|
||||||
32
interfaces/main.go
Normal file
32
interfaces/main.go
Normal 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
3
maps/go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module lovelynet.net/maps
|
||||||
|
|
||||||
|
go 1.17
|
||||||
19
maps/main.go
Normal file
19
maps/main.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user