-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
48 lines (38 loc) · 1.22 KB
/
map.go
File metadata and controls
48 lines (38 loc) · 1.22 KB
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
package main
import "fmt"
func main() {
// map is a collection of key-value pairs
// create a map with make function
person := make(map[string]string)
// add key-value pairs to the map
person["name"] = "Muhammad Luthfi"
person["address"] = "Jakarta"
person["occupation"] = "Software Engineer"
println("Name:", person["name"])
println("Address:", person["address"])
println("Occupation:", person["occupation"])
fmt.Println(person) // fo display more details use fmt.Println
fmt.Println("---------------------------------------------------")
// create a new map with map literal
books := map[string]string{
"title": "The Go Programming Language",
"author": "Alan A. A. Donovan",
"year": "2015",
}
println("Book Title:", books["title"])
println("Book Author:", books["author"])
println("Publication Year:", books["year"])
fmt.Println(books)
// built-in function
// len() to get the number of key-value pairs in the map
fmt.Println("Number of books info:", len(books))
// delete key-value pair from the map
delete(books, "year")
fmt.Println(books)
// change map value
books["author"] = "Brian W. Kernighan"
fmt.Println(books)
// add map value
books["publisher"] = "Addison-Wesley"
fmt.Println(books)
}