Skip to content

Commit

Permalink
Merge pull request #3 from 0bvim/feat/section15
Browse files Browse the repository at this point in the history
feat/section15
  • Loading branch information
0bvim authored Oct 21, 2024
2 parents 2f35b7d + e50899e commit 3b671e7
Show file tree
Hide file tree
Showing 3 changed files with 393 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/udemy/go_ted/section15/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import "fmt"

func main() {

mapping := map[string]int{
"Todd": 42,
"Lila": 22,
"Stete": 32,
}
// 116 - map
{
fmt.Printf("mapping: %v\n", mapping)
fmt.Printf("mapping Lila: %v\n", mapping["Lila"])

another := make(map[string]int)
another["Lucas"] = 28
another["Steph"] = 37
fmt.Printf("another: %v\n", another)
fmt.Println("--------------")
}
// 117 adding element in a map and using range
{
for key, value := range mapping {
fmt.Println(key, value)
}

for _, value := range mapping {
fmt.Println(value)
}

for key, _ := range mapping {
fmt.Println(key)
}
fmt.Println("--------------")
}
// 118 delete item from a map
{
delete(mapping, "Todd")
delete(mapping, "Todd") // using twice don't make program panic, just ignore even if you try to print or something else.

fmt.Printf("mapping: %v\n", mapping["Todd"])
fmt.Printf("mapping: %v\n", mapping)
fmt.Println("--------------")
}
// 118 using ok notation to verify valid values in map
{
// if you liik up a non-existent key, the zero value will be returned as the value associated
// with that non-existent key
if v, ok := mapping["Lila"]; !ok {
fmt.Println("key didn't exist")
} else {
fmt.Println(v)
}

delete(mapping, "Lila")
if v, ok := mapping["Lila"]; ok {
fmt.Println(v)
} else {
fmt.Println("key didn't exist")
}
}
}
Loading

0 comments on commit 3b671e7

Please sign in to comment.