what is the maximum number of key that golang map can have?

3,459 views
Skip to first unread message

Gyu-Ho Lee

unread,
Feb 7, 2015, 11:17:56 PM2/7/15
to golan...@googlegroups.com
Is there any cap on Go's built-in map or slice? I know slice can have cap but what is the maximum number of elements that we can have in one map and slice?

I am thinking about having my entries in Redis or other key-value storage until I have too many entries to store in memory.

Any advice will be appreciated. Thanks!

Caleb Spare

unread,
Feb 7, 2015, 11:20:15 PM2/7/15
to Gyu-Ho Lee, golang-nuts
The length and capacity of slices and maps are ints, so I believe that
the int size on your platform (as well as available memory, of course)
are the only limitations.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Caleb Spare

unread,
Feb 7, 2015, 11:20:44 PM2/7/15
to Gyu-Ho Lee, golang-nuts
(not that maps have a cap)

Gyu-Ho Lee

unread,
Feb 7, 2015, 11:22:31 PM2/7/15
to Caleb Spare, golang-nuts
I see. Thanks! I learned it today.

https://golang.org/ref/spec#Numeric_types

Harmen B

unread,
Feb 8, 2015, 9:29:35 AM2/8/15
to Gyu-Ho Lee, Caleb Spare, golang-nuts
Be careful with the garbage collector, though. If you keep millions of elements around the garbage collector might give you longer pauses than you would like (but this behavior is likely to change in next Go versions, for the better).

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Dave Cheney

unread,
Feb 8, 2015, 6:41:41 PM2/8/15
to golan...@googlegroups.com
There is no limit the number of elements you can store in a map save the limitations of your hardware or the current maximum size of the heap.

Gyu-Ho Lee

unread,
Feb 8, 2015, 7:45:11 PM2/8/15
to golan...@googlegroups.com
I see. Yeah I see that after storing over 2 million, my program pauses a bit long.

Thanks, Dave and Harmen.
Reply all
Reply to author
Forward
0 new messages