Go Maps: Difference between revisions
Jump to navigation
Jump to search
Line 22: | Line 22: | ||
Note that maps must be initialized before attempting to write into them, see [[Go Maps#Assigning_a_Value_to_an_Uninitialized_Map|Assinging a Value to an Unitialized Map]]. | Note that maps must be initialized before attempting to write into them, see [[Go Maps#Assigning_a_Value_to_an_Uninitialized_Map|Assinging a Value to an Unitialized Map]]. | ||
Declaration and initialization: | |||
<pre> | |||
var m map[string]string = make(map[string]string) | |||
</pre> | |||
==Short Declaration== | |||
<pre> | |||
m := make(map[string]string) | |||
</pre> | |||
==Short Declaration== | ==Short Declaration== |
Revision as of 02:37, 28 March 2016
Internal
Overview
A map is an unordered collection of key-value pairs.
Declaration
Long Declaration
var map_identifier map[key_type]value_type
Example of a map of string to ints:
var m map[string]int
Note that maps must be initialized before attempting to write into them, see Assinging a Value to an Unitialized Map.
Declaration and initialization:
var m map[string]string = make(map[string]string)
Short Declaration
m := make(map[string]string)
Short Declaration
Map Operators and Functions
Indexing Operator
Indexing operator [] returns the value corresponding to the specified key. If the key does not exist, the zero value for the value type is returned.
Assigning a Value to an Uninitialized Map
If the indexing operator is also used to assign a value to a key for an uninitialized map, we get a runtime error:
panic: assignment to entry in nil map