JIYIK CN >

Current Location:Home > Learning > PROGRAM > Go >

GoLang elements are maps of maps

Author:JIYIK Last Updated:2025/04/15 Views:

This article introduces how to create a Map of Maps in Go language.


GoLang Map of Map

Maps of Maps or nested maps are those maps whose key-value pairs are also maps. In Go language, you can create Maps of Maps.

When we define a Map, we have to define the type of key and value. In this case, the type of key can be int, string or any other type and the type of value will again be map.

grammar:

var DemoMap = map[string]map[string]string{}
// 或者

var DemoMap = map[int]map[string]string{}

As you can see, DemoMap defines a key string, int type, and a value of map type. Let's take an example.

Code:

package main
import "fmt"

func main() {
    DemoMap := map[int]map[string]string{
        1: {
            "one":   "Jiyik1",
            "two":   "Jiyik2",
            "three": "Jiyik3",
        },
        2: {
            "four": "Jiyik4",
            "five": "Jiyik5",
            "Six":  "Jiyik6",
        },
        3: {
            "seven": "Jiyik7",
            "eight": "Jiyik8",
            "nine":  "Jiyik9",
        },
        4: {
            "ten":    "Jiyik10",
            "eleven": "Jiyik11",
            "twelve": "Jiyik12",
        },
    }
    fmt.Println(DemoMap)
}

The above code creates a Map containing key integers and value Map<int> and then prints the Map.

Output:

map[
    1:map[one:Jiyik1 three:Jiyik3 two:Jiyik2]
    2:map[Six:Jiyik6 five:Jiyik5 four:Jiyik4]
    3:map[eight:Jiyik8 nine:Jiyik9 seven:Jiyik7]
    4:map[eleven:Jiyik11 ten:Jiyik10 twelve:Jiyik12]]

Deleting a Nested Map

We can use GoLang's delete()method to delete a nested Map. It takes two parameters, one is the key parent mapand the other is the nested map key.

Let’s take an example.

Code:

package main
import "fmt"

func main() {
    DemoMap := map[int]map[string]string{
        1: {
            "one":   "Jiyik1",
            "two":   "Jiyik2",
            "three": "Jiyik3",
        },
        2: {
            "four": "Jiyik4",
            "five": "Jiyik5",
            "Six":  "Jiyik6",
        },
        3: {
            "seven": "Jiyik7",
            "eight": "Jiyik8",
            "nine":  "Jiyik9",
        },
        4: {
            "ten":    "Jiyik10",
            "eleven": "Jiyik11",
            "twelve": "Jiyik12",
        },
    }
    delete(DemoMap, 3)
    fmt.Println(DemoMap)
}

The above code will remove the nested Map at key 3 from the parent Map.

Output:

map[
    1:map[one:Jiyik1 three:Jiyik3 two:Jiyik2]
    2:map[Six:Jiyik6 five:Jiyik5 four:Jiyik4]
    4:map[eleven:Jiyik11 ten:Jiyik10 twelve:Jiyik12]]

Similarly, we can remove a member of a nested Map using the same delete()method where the first argument will be the nested Map and the second argument will be the key we want to remove.

Code:

package main
import "fmt"

func main() {
    DemoMap := map[int]map[string]string{
        1: {
            "one":   "Jiyik1",
            "two":   "Jiyik2",
            "three": "Jiyik3",
        },
        2: {
            "four": "Jiyik4",
            "five": "Jiyik5",
            "Six":  "Jiyik6",
        },
        3: {
            "seven": "Jiyik7",
            "eight": "Jiyik8",
            "nine":  "Jiyik9",
        },
        4: {
            "ten":    "Jiyik10",
            "eleven": "Jiyik11",
            "twelve": "Jiyik12",
        },
    }
    delete(DemoMap[3], "seven")
    fmt.Println(DemoMap)
}

The above code will delete the member with key 3 and member key 7 in the nested Map.

Output:

map[
    1:map[one:Jiyik1 three:Jiyik3 two:Jiyik2]
    2:map[Six:Jiyik6 five:Jiyik5 four:Jiyik4]
    3:map[eight:Jiyik8 nine:Jiyik9]
    4:map[eleven:Jiyik11 ten:Jiyik10 twelve:Jiyik12]]

Iterating over a nested Map

To iterate over a nested Map, we have to use for loop giving the index number of the Map. Let us take an example of iterating over a nested Map.

Code:

package main
import "fmt"

func main() {
    DemoMap := map[int]map[string]string{
        1: {
            "one":   "Jiyik1",
            "two":   "Jiyik2",
            "three": "Jiyik3",
        },
        2: {
            "four": "Jiyik4",
            "five": "Jiyik5",
            "Six":  "Jiyik6",
        },
        3: {
            "seven": "Jiyik7",
            "eight": "Jiyik8",
            "nine":  "Jiyik9",
        },
        4: {
            "ten":    "Jiyik10",
            "eleven": "Jiyik11",
            "twelve": "Jiyik12",
        },
    }
    for key, value := range DemoMap[3] {
        fmt.Println(key, value)
    }
}

The above code will iterate over the Map at the third position in the parent Map.

Output:

seven Jiyik7
eight Jiyik8
nine Jiyik9

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Install GoLang using Brew

Publish Date:2025/04/15 Views:82 Category:Go

This article describes how to install GoLang using Brew on Linux or macOS. Install GoLang using Brew brew installs missing packages in Linux and macOS. It makes it easy to install GoLang on Linux or macOS. Follow the steps below to install

GoLang RWMutex Detailed Introduction

Publish Date:2025/04/15 Views:116 Category:Go

This article introduces how to use rwmutex in Go language. Go language RWMutex mutex is the abbreviation of mutual exclusion, which is used to keep track of which thread has accessed a variable at any time. Mutex is a data structure provide

Enabling CORS in GoLang

Publish Date:2025/04/15 Views:102 Category:Go

This article describes how to enable and use CORS in GoLang. Go language CORS Cross-origin resource sharing (CORS) is a process based on HTTP headers that defines the origins from which browsers are allowed to load and use resources. CORS i

Function pointers in Go

Publish Date:2025/04/15 Views:83 Category:Go

A pointer in Go is a special-purpose variable that stores the memory address of other variables and the point where the memory is located. It can also access the value stored in that memory location. Pointer declaration in Go var pointer_na

Duration in Go

Publish Date:2025/04/15 Views:89 Category:Go

Go duration conversion can be done in a variety of ways. The time library and the time.duration method are often used to calculate and display time. Note that duration refers to the time elapsed between two defined time objects, as an int64

Create a new directory in Go

Publish Date:2025/04/15 Views:112 Category:Go

Go has many ways to create directories, one of which is os.Mkdir . We can assign specified names and permission bits to the newly formed directory. os.MkdirAll Useful for recursively creating multiple directories, including any missing pare

How to read a file into a string in GoLang

Publish Date:2025/04/15 Views:90 Category:Go

The Go language provides a lot of file manipulation tools, one of which is how to read files into strings. ioutil.ReadFile() , File.Read() , buf.ReadFrom() and strings.Builder are just a few of the methods that can be used to efficiently wr

Declaring constant Map in Go

Publish Date:2025/04/15 Views:166 Category:Go

Map is a collection of key-value pairs which can be sorted in any order. It assigns values ​​to keys. The keys in a Map are always unique, but the values ​​are not always unique. The map data structure is used for fast key-based dat

Merge two maps in Golang

Publish Date:2025/04/15 Views:164 Category:Go

This article describes how to merge two maps in Golang. Golang does not provide any in-built functionality to merge mappings, but this can be achieved using Copy method or by following the corresponding procedure. Let’s try both the metho

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial