Initialize a nested map

8,313 views
Skip to first unread message

Archos

unread,
Jan 11, 2012, 6:11:49 AM1/11/12
to golang-nuts
Is correct to use a nested map like this one? If so, how is
initialized?

var pointers map[int]map[int][]string

Jan Mercl

unread,
Jan 11, 2012, 6:26:08 AM1/11/12
to golan...@googlegroups.com
On Wednesday, January 11, 2012 12:11:49 PM UTC+1, Archos wrote:
Is correct to use a nested map like this one?

Don't know if correct, but valid it is.
 
If so, how is
initialized?

var pointers map[int]map[int][]string

kortschak

unread,
Jan 11, 2012, 6:29:18 AM1/11/12
to golang-nuts
package main

import "fmt"

func main() {
outer := make(map[int]map[int][]string)
outer[1] = make(map[int][]string)
outer[1][2] = []string{} // or equivalent

fmt.Println(outer)

Archos

unread,
Jan 11, 2012, 6:39:14 AM1/11/12
to golang-nuts
My failure is that I wasn't initializing the slice, in my code. Thanks
to both!

Sam Nardoni

unread,
Jan 12, 2012, 4:02:43 AM1/12/12
to golan...@googlegroups.com
While correct, it might be worth breaking it down in to smaller chunks:

type int2strings map[int][]string
map[int]int2strings

o...@streamrail.com

unread,
Sep 29, 2014, 12:04:32 PM9/29/14
to golan...@googlegroups.com
+1 for using types.

By the way, there's a nice nested data structures golang tutorial here:

carrie....@gmail.com

unread,
Jun 10, 2020, 9:03:39 PM6/10/20
to golang-nuts
This is so helpful, even in 2020.  Thank you!
Reply all
Reply to author
Forward
0 new messages