Home > Backend Development > Golang > How Do I Avoid Runtime Panics When Using Nested Maps in Go?

How Do I Avoid Runtime Panics When Using Nested Maps in Go?

DDD
Release: 2024-12-06 06:30:12
Original
218 people have browsed it

How Do I Avoid Runtime Panics When Using Nested Maps in Go?

Nested Maps in Go

In Go, maps are a powerful data structure that allow you to store key-value pairs. Nested maps, where the values are themselves maps, can be a useful way to organize complex data.

Problem

However, some developers have encountered issues when working with nested maps. For instance, the following code snippet runs successfully:

func main() {
    var data = map[string]string{}
    data["a"] = "x"
    data["b"] = "x"
    data["c"] = "x"
    fmt.Println(data)
}
Copy after login

And so does the following:

func main() {
    var data = map[string][]string{}
    data["a"] = append(data["a"], "x")
    data["b"] = append(data["b"], "x")
    data["c"] = append(data["c"], "x")
    fmt.Println(data)
}
Copy after login

But the following code panics at runtime:

func main() {
    var data = map[string]map[string]string{}
    data["a"]["w"] = "x"
    data["b"]["w"] = "x"
    data["c"]["w"] = "x"
    fmt.Println(data)
}
Copy after login

Explanation

The issue arises because the zero value for map types in Go is nil, indicating that the map is uninitialized. Attempting to access or store values in a nil map results in a runtime panic.

In the last example, the (outer) data map is initialized, but it has no entries. When you index it like data["a"], since there is no entry with the "a" key yet, indexing it returns the zero value of the value type, which is nil for maps. Assigning to data"a" then becomes an attempt to assign to a nil map, resulting in a panic.

Solution

To avoid this issue, you must initialize a map before storing elements in it. This can be done in several ways:

  • Initialize the outer map with a composite literal:
var data = map[string]map[string]string{
    "a": {},
    "b": {},
    "c": {},
}
Copy after login
  • Use the make function:
var data = make(map[string]map[string]string)
Copy after login
  • Initialize the inner maps individually:
var data = map[string]map[string]string{}

data["a"] = map[string]string{}
data["b"] = make(map[string]string)
data["c"] = make(map[string]string)
Copy after login

After initialization, you can safely store values in the nested maps.

The above is the detailed content of How Do I Avoid Runtime Panics When Using Nested Maps in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template