Associative array/Creation: Difference between revisions

→‎{{header|Go}}: gofmt; show getting and deleting
(→‎{{header|Go}}: gofmt; show getting and deleting)
Line 921:
Allowable key types are those with == and != operators. This includes is boolean, numeric, string, pointer, channel, and interface types. It also includes structs and arrays containing only these types. Disallowed as map keys are all slice, function, and map types.
<lang go>// declare a nil map variable, for maps from string to int
var x map[string] int
 
// make an empty map
x = make(map[string] int)
 
// make an empty map with an initial capacity
x = make(map[string] int, 42)
 
// set a value
x["foo"] = 3
 
// getting values
y1 := x["bar"] // zero value returned if no map entry exists for the key
y2, ok := x["bar"] // ok is a boolean, true if key exists in the map
 
// removing keys
delete(x, "foo")
 
// make a map with a literal
x = map[string] int {
"foo": 2, "bar": 42, "baz": -1,
}</lang>
 
Anonymous user