You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was implementing an algorithm that uses a map of maps (e.g. map[int]map[string][]int ) but when trying to assign a slice of integers to key value I got an error message about assigning entry to nil map.
Sample code for better understanding:
data := make(map[int]map[string][]int)
...
for id, each := range data {
values, okToInsert := ready()
if oktoInsert {
newID := id + 5
// Tag A
data[newID]["db"] = values // Error raised
}
}
To overcome this error I had to put a verification, in Tag A, for the availability of the key map like:
if _, ok := data[newID]; !ok {
data[newID] = make(map[string][]int)
}
Why can't 'make' ensure this allocation ?
Thank you guys
The text was updated successfully, but these errors were encountered:
I was implementing an algorithm that uses a map of maps (e.g. map[int]map[string][]int ) but when trying to assign a slice of integers to key value I got an error message about assigning entry to nil map.
Sample code for better understanding:
data := make(map[int]map[string][]int)
...
for id, each := range data {
values, okToInsert := ready()
if oktoInsert {
newID := id + 5
// Tag A
data[newID]["db"] = values // Error raised
}
}
To overcome this error I had to put a verification, in Tag A, for the availability of the key map like:
if _, ok := data[newID]; !ok {
data[newID] = make(map[string][]int)
}
Why can't 'make' ensure this allocation ?
Thank you guys
The text was updated successfully, but these errors were encountered: