From d1a26a5d92a7bca1b5b367a14f1eede994ce42a5 Mon Sep 17 00:00:00 2001 From: Johannes Rothe Date: Sat, 12 Feb 2022 00:22:02 +0100 Subject: [PATCH] maps, interfaces, structs --- interfaces/go.mod | 3 +++ interfaces/main.go | 29 +++++++++++++++++++++++++++++ interfaces_http/go.mod | 3 +++ interfaces_http/main.go | 16 ++++++++++++++++ map/go.mod | 3 +++ map/main.go | 28 ++++++++++++++++++++++++++++ structs/main.go | 2 +- 7 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 interfaces/go.mod create mode 100644 interfaces/main.go create mode 100644 interfaces_http/go.mod create mode 100644 interfaces_http/main.go create mode 100644 map/go.mod create mode 100644 map/main.go diff --git a/interfaces/go.mod b/interfaces/go.mod new file mode 100644 index 0000000..5ede8e6 --- /dev/null +++ b/interfaces/go.mod @@ -0,0 +1,3 @@ +module interfaces + +go 1.17 diff --git a/interfaces/main.go b/interfaces/main.go new file mode 100644 index 0000000..593591f --- /dev/null +++ b/interfaces/main.go @@ -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!" +} diff --git a/interfaces_http/go.mod b/interfaces_http/go.mod new file mode 100644 index 0000000..b53fa37 --- /dev/null +++ b/interfaces_http/go.mod @@ -0,0 +1,3 @@ +module interfaces_http + +go 1.17 diff --git a/interfaces_http/main.go b/interfaces_http/main.go new file mode 100644 index 0000000..4f34616 --- /dev/null +++ b/interfaces_http/main.go @@ -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) +} diff --git a/map/go.mod b/map/go.mod new file mode 100644 index 0000000..be693fe --- /dev/null +++ b/map/go.mod @@ -0,0 +1,3 @@ +module map + +go 1.17 diff --git a/map/main.go b/map/main.go new file mode 100644 index 0000000..83b0759 --- /dev/null +++ b/map/main.go @@ -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) + } +} diff --git a/structs/main.go b/structs/main.go index 03ef969..415886d 100644 --- a/structs/main.go +++ b/structs/main.go @@ -22,7 +22,7 @@ func main() { zipCode: 1234, }, } - (&person1).updateName("asd") + person1.updateName("asd") person1.print() }