-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaps.go
More file actions
50 lines (37 loc) · 856 Bytes
/
Copy pathMaps.go
File metadata and controls
50 lines (37 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
)
func main() {
myMap := make(map[string]int)
for {
var key string
fmt.Print("Enter a key (or 'quit' to exit): ")
fmt.Scan(&key)
if key == "quit" {
break
}
var value int
fmt.Print("Enter a value: ")
fmt.Scan(&value)
myMap[key] = value
}
fmt.Println("Map after user input:", myMap)
fmt.Print("Enter a key to get its value: ")
var key string
fmt.Scan(&key)
if val, ok := myMap[key]; ok {
fmt.Printf("Value of %s: %d\n", key, val)
} else {
fmt.Printf("Key %s not found in the map\n", key)
}
fmt.Print("Enter a key to delete: ")
fmt.Scan(&key)
delete(myMap, key)
fmt.Println("Map after deleting:", myMap)
fmt.Println("Iterating over the map:")
for key, value := range myMap {
fmt.Printf("Key: %s, Value: %d\n", key, value)
}
fmt.Println("Length of the map:", len(myMap))
}