Home > Backend Development > Golang > Why am I getting a 'Runtime Error: Assignment to Entry in Nil Map' while creating a slice of maps in Go?

Why am I getting a 'Runtime Error: Assignment to Entry in Nil Map' while creating a slice of maps in Go?

Patricia Arquette
Release: 2024-11-04 12:40:29
Original
849 people have browsed it

Why am I getting a

Runtime Error: "Assignment to Entry in Nil Map" During Slice of Maps Creation

In an attempt to generate a slice of maps, an "assignment to entry in nil map" runtime error arises. Despite the code compiling successfully, this error hampers the program's execution.

The goal is to create an array of maps, with each map containing two indices: "Id" and "Investor." The code attempts this:

<code class="go">for _, row := range rows {
    invs := make([]map[string]string, length)
    for i := 0; i < length; i++ {
        invs[i] = make(map[string]string)
        invs[i]["Id"] = inv_ids[i]
        invs[i]["Investor"] = inv_names[i]
    }
    return(msg)
}</code>
Copy after login

Initially, an attempt was made to address the problem with the following code:

<code class="go">invs := make([]make(map[string]string), length)</code>
Copy after login

However, it did not resolve the issue.

A Comprehensive Solution

To create a slice of maps effectively, one must employ a different approach. Consider the following example:

<code class="go">package main

import "fmt"

func main() {
    a := make([]map[string]int, 100)
    for i := 0; i < 100; i++ {
        a[i] = map[string]int{"id": i, "investor": i}
    }
    fmt.Println(a)
}</code>
Copy after login

Instead of using a loop to assign values to each map, one can use a composite literal:

<code class="go">invs[i] = map[string]string{"Id": inv_ids[i], "Investor": inv_names[i]}</code>
Copy after login

Alternative Solution Using Structs

For a more idiomatic approach, using a struct to represent an investor is advisable:

<code class="go">package main

import (
    "fmt"
    " strconv"
)

type Investor struct {
    Id   int
    Name string
}

func main() {
    a := make([]Investor, 100)
    for i := 0; i < 100; i++ {
        a[i] = Investor{Id: i, Name: "John" + strconv.Itoa(i)}
        fmt.Printf("%#v\n", a[i])
    }
}</code>
Copy after login

By employing these techniques, the runtime error can be eliminated, and the desired slice of maps or an array of structs can be obtained, depending on the specific requirements of the program.

The above is the detailed content of Why am I getting a 'Runtime Error: Assignment to Entry in Nil Map' while creating a slice of 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template