Skip to content

runtime: Memory allocation for a map of map #10053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mDomingues opened this issue Mar 2, 2015 · 2 comments
Closed

runtime: Memory allocation for a map of map #10053

mDomingues opened this issue Mar 2, 2015 · 2 comments

Comments

@mDomingues
Copy link

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

@dvyukov
Copy link
Member

dvyukov commented Mar 2, 2015

You want outer make to allocate inner maps for what set of keys?

@ianlancetaylor
Copy link
Member

Go doesn't work this way. The zero value for a map can not be used directly; you have to run make first. This is not going to change.

@mikioh mikioh changed the title Memory allocation for a map of map runtime: Memory allocation for a map of map Mar 5, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants