maps, interfaces, structs
This commit is contained in:
parent
37979af331
commit
d1a26a5d92
3
interfaces/go.mod
Normal file
3
interfaces/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module interfaces
|
||||
|
||||
go 1.17
|
29
interfaces/main.go
Normal file
29
interfaces/main.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type bot interface {
|
||||
getGreeting() string
|
||||
}
|
||||
|
||||
type englishBot struct{}
|
||||
type spanishBot struct{}
|
||||
|
||||
func main() {
|
||||
eb := englishBot{}
|
||||
sb := spanishBot{}
|
||||
printGreeting(eb)
|
||||
printGreeting(sb)
|
||||
}
|
||||
|
||||
func printGreeting(b bot) {
|
||||
fmt.Println(b.getGreeting())
|
||||
}
|
||||
|
||||
func (englishBot) getGreeting() string {
|
||||
return "Hi there!"
|
||||
}
|
||||
|
||||
func (spanishBot) getGreeting() string {
|
||||
return "Hola!"
|
||||
}
|
3
interfaces_http/go.mod
Normal file
3
interfaces_http/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module interfaces_http
|
||||
|
||||
go 1.17
|
16
interfaces_http/main.go
Normal file
16
interfaces_http/main.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
resp, err := http.Get("https://google.com")
|
||||
if err != nil {
|
||||
fmt.Println("Error!")
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println(resp)
|
||||
}
|
3
map/go.mod
Normal file
3
map/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module map
|
||||
|
||||
go 1.17
|
28
map/main.go
Normal file
28
map/main.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// Method 1
|
||||
//var colors map[string]string
|
||||
|
||||
// Method2
|
||||
//colors := make(map[string]string)
|
||||
|
||||
colors := map[string]string{
|
||||
"red": "#ff0000",
|
||||
"green": "#gr0000",
|
||||
"white": "#fffff",
|
||||
}
|
||||
|
||||
//colors["white"] = "#ffffff"
|
||||
|
||||
//delete(colors, "white")
|
||||
printMap(colors)
|
||||
}
|
||||
|
||||
func printMap(c map[string]string) {
|
||||
for color, hex := range c {
|
||||
fmt.Println("Hex code for", color, "is", hex)
|
||||
}
|
||||
}
|
@ -22,7 +22,7 @@ func main() {
|
||||
zipCode: 1234,
|
||||
},
|
||||
}
|
||||
(&person1).updateName("asd")
|
||||
person1.updateName("asd")
|
||||
person1.print()
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user