Home > Backend Development > Golang > Why Do Nested Maps in Go Cause Runtime Panics When Accessed Directly, But Not When Using Append or Initialization?

Why Do Nested Maps in Go Cause Runtime Panics When Accessed Directly, But Not When Using Append or Initialization?

Mary-Kate Olsen
Release: 2024-12-04 06:17:11
Original
819 people have browsed it

Why Do Nested Maps in Go Cause Runtime Panics When Accessed Directly, But Not When Using Append or Initialization?

Nested Maps in Golang: Anomalies Explored

When exploring nested maps in Golang, a series of code examples reveal apparent discrepancies:

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

This code runs successfully.

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

You can also do this.

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

This can also be done.

However, when I run the code below I get an error.

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

There's a problem.

Investigating the cause

The cause of this problem is that the zero value of the map type is nil. Zero value is uninitialized. You cannot store values ​​in a nil map. This is a runtime panic.

In the last example, we initialized the (external) data map, but there are no entries. If you index like data["a"], there is no entry for the "a" key yet, so the indexing returns a zero value with a value type of nil. Therefore, if you try to assign to data"a", a runtime panic will occur.

The map must be initialized before storing any elements. For example:

var data = map[string]map[string]string{}

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

data["a"]["w"] = "x"
data["b"]["w"] = "x"
data["c"]["w"] = "x"
fmt.Println(data)
Copy after login

Output (Try it in Go Playground):

map[a:map[w:x] b:map[w:x] c:map[w:x]]
Copy after login

If you declare and initialize a variable map type using a compound literal, that is also considered an initialization. Masu.

var data = map[string]map[string]string{
    "a": map[string]string{},
    "b": map[string]string{},
    "c": map[string]string{},
}

data["a"]["w"] = "x"
data["b"]["w"] = "x"
data["c"]["w"] = "x"
fmt.Println(data)
Copy after login

The output is the same. Try it at Go Playground.

The above is the detailed content of Why Do Nested Maps in Go Cause Runtime Panics When Accessed Directly, But Not When Using Append or Initialization?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template