SoFunction
Updated on 2025-03-05

9 common examples of maps in Golang

1. Basic use

package main

import (
 "fmt"
)

func main() {
 stu := make(map[string]int, 2)
 stu["xiaoming"] = 25
 stu["xiaohua"] = 23
 (stu)
 (stu["xiaoming"])
 ("type of a: %T", stu)
}

Output:

map[xiaohua:23 xiaoming:25]
25
type of a: map[string]int

2. Initialize together while declaring

package main

import (
 "fmt"
)

func main() {

 stu := map[string]int{
  "xiaoming": 25,
  "xiaohua":  22,
 }
 (stu)
 (stu["xiaoming"])
 (stu["xiaohua"])
}

Output:

map[xiaohua:22 xiaoming:25]
25
22

3. Determine whether the key exists

package main

import (
 "fmt"
)

func main() {

 stu := map[string]int{
  "xiaoming": 25,
  "xiaohua":  22,
 }

 v1, result1 := stu["xiaoming"]
 v2, result2 := stu["xiaohuang"]
 (result1, v1)
 (result2, v2)

 if result1 {
  ("yes")
 } else {
  ("no")
 }

 if result2 {
  ("yes")
 } else {
  ("no")
 }
}

Output:

true 25
false 0
yes
no

If the key exists, result1 is true, otherwise, result2 can be seen.

4. Iterate over map

package main

import (
 "fmt"
)

func main() {
 stu := map[string]int{
  "xiaoming":  25,
  "xiaohua":   22,
  "xiaozhang": 23,
  "xiaoshi":   21,
  "xiaoyu":    18,
 }
 for k, v := range stu {
  (k, v)
 }
}

Output:

xiaoming 25
xiaohua 22
xiaozhang 23
xiaoshi 21
xiaoyu 18

You can also just traverse the key

for k := range stu {
    (k)
}

5. Delete the k-v pair

Use the built-in delete function to delete

package main

import (
 "fmt"
)

func main() {
 stu := map[string]int{
  "xiaoming":  25,
  "xiaohua":   22,
  "xiaozhang": 23,
  "xiaoshi":   21,
  "xiaoyu":    18,
 }
 (stu)
 delete(stu, "xiaoshi")
 (stu)
}

Output:

map[xiaohua:22 xiaoming:25 xiaoshi:21 xiaoyu:18 xiaozhang:23]
map[xiaohua:22 xiaoming:25 xiaoyu:18 xiaozhang:23]

It is unordered, how to make it traverse maps in the specified order

"Unordered traversal demonstration"

package main

import (
 "fmt"
 "math/rand"
 "time"
)

func main() {
 (().UnixNano())

 var hostMap = make(map[string]string, 10)

 for i := 0; i < 10; i++ {
  key := ("host%02d", i)
  value := (100)
  ip := ("10.1.1.%d", value)
  hostMap[key] = ip
 }
 for k, v := range hostMap {
  (k, v)
 }
}

Output:

host03 10.1.1.32
host00 10.1.1.0
host02 10.1.1.37
host05 10.1.1.97
host06 10.1.1.61
host07 10.1.1.62
host08 10.1.1.28
host09 10.1.1.40
host01 10.1.1.62
host04 10.1.1.70

Have you noticed it? Maps are disordered. Although host01-host09 is assigned to map in sequence, it is not ordered when traversing the values. Moreover, the order of each traversal is different.

"Implement orderly traversal"

package main

import (
 "fmt"
 "math/rand"
 "sort"
 "time"
)

func main() {
 (().UnixNano())

 var hostMap = make(map[string]string, 10)

 for i := 0; i &lt; 10; i++ {
  key := ("host%02d", i)
  value := (100)
  ip := ("10.1.1.%d", value)
  hostMap[key] = ip
 }

 var hostSlice = make([]string, 0, 200)

 // Append the key in the hostMap to hostSlice (slice) for k := range hostMap {
  hostSlice = append(hostSlice, k)
 }

 // Use the built-in sort function to sort the slice (hostSlice) (sorting based on key, and the key has just been appended to the slice) (hostSlice)

 for _, k := range hostSlice {
  (k, hostMap[k])
 }
}

Output:

host00 10.1.1.87
host01 10.1.1.98
host02 10.1.1.93
host03 10.1.1.4
host04 10.1.1.1
host05 10.1.1.28
host06 10.1.1.11
host07 10.1.1.43
host08 10.1.1.31
host09 10.1.1.83

Store as element into slice

package main

import (
 "fmt"
)

func main() {
 // Create a slice that stores map type elements, with a maximum expansion capacity of 3 dataSlice := make([]map[string]string, 3)
 (dataSlice)

 ("------------")
 // Create a map with both key and value of string at the index 0 of the slice, with a capacity of 2 dataSlice[0] = make(map[string]string, 2)
 // Assign map type data to the index 0 position of the slice, the key is user and the value is root dataSlice[0]["user"] = "root"
 dataSlice[0]["pwd"] = "abc123"
 (dataSlice)

 ("------------")
 for i := range dataSlice {
  (dataSlice[i])
 }

 ("------------")
 for i := range dataSlice {
  (dataSlice[i]["user"], dataSlice[i]["pwd"])
 }
}

Output:

[map[] map[] map[]]
------------
[map[pwd:abc123 user:root] map[] map[]]
------------
map[pwd:abc123 user:root]
map[]
map[]
------------
root abc123

In the above example, only the map element is stored at index 0 of the slice, and index 1 and 2 do not.

8. Slice as the value of the map, and the key of the map is a string

package main

import "fmt"

func main() {
 // Declare the slice of the string type and initialize two values value := []string{"192.168.10.12", "10.1.1.23"}

 //Declare the map of the key type as a string and the value type as a slice, and initialize a pair of keys and values data := map[string][]string{
  "ip": value,
 }
 (data)
 (data["ip"])
}

Output:

map[ip:[192.168.10.12 10.1.1.23]]
[192.168.10.12 10.1.1.23]

Value as map

package main

import "fmt"

func main() {
 value := map[string]string{
  "Manage IP": "10.1.2.39",
  "Business IP": "192.168.12.56",
 }

 a := map[string]map[string]string{
  "nginx": value,
 }

 (a)
 (a["nginx"])
 (a["nginx"]["Manage IP"])
 (a["nginx"]["Business IP"])
}

Output:

map[nginx:map[Business IP:192.168.12.56 Management IP:10.1.2.39]]]
map[Business IP: 192.168.12.56 Management IP: 10.1.2.39]
10.1.2.39
192.168.12.56

This is the end of this article about 9 common examples of maps shared in Golang. For more related Golang map content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!