what is difference beetween make(map[string]int) and map[string]int

234 views
Skip to first unread message

alio...@gmail.com

unread,
Apr 17, 2015, 11:04:25 AM4/17/15
to golan...@googlegroups.com
what is difference of these in go lang

    array := make(map[string]int)
    array2 := map[string]int

i am printing both variables like `fmt.Println(array, array2)` then i cannot any differences beetween these

is the only difference make function have second parameter that we can define number of instance ?

Shawn Milochik

unread,
Apr 17, 2015, 11:17:08 AM4/17/15
to golan...@googlegroups.com
On Fri, Apr 17, 2015 at 11:04 AM, <alio...@gmail.com> wrote:
what is difference of these in go lang

    array := make(map[string]int)
    array2 := map[string]int


Make allocates memory. The "array2" line doesn't compile as shown. It's an incomplete map literal.

You can make it compile by changing it to "var array2 map[string]int," but then when you try assigning something to it you'll get a panic because it's a "nil map." So you'd have to "make" a map and assign it to that variable.

To complete the literal, you could do something like this:

a1 := map[string]int{"foo": 42}
fmt.Println(a1)


Nigel Tao

unread,
Apr 19, 2015, 11:37:26 PM4/19/15
to alio...@gmail.com, golang-nuts
The make form lets you supply a size hint. The literal form lets you
specify initial elements. If you're doing neither, then

m0 := make(map[string]int)
m1 := map[string]int{}

are equivalent.
Reply all
Reply to author
Forward
0 new messages