RosettaCodeData/Task/Associative-array-Creation/Go/associative-array-creation.go

24 lines
517 B
Go
Raw Permalink Normal View History

2013-04-10 14:58:50 -07:00
// declare a nil map variable, for maps from string to int
2015-02-20 00:35:01 -05:00
var x map[string]int
2013-04-10 14:58:50 -07:00
// make an empty map
2015-02-20 00:35:01 -05:00
x = make(map[string]int)
2013-04-10 14:58:50 -07:00
// make an empty map with an initial capacity
2015-02-20 00:35:01 -05:00
x = make(map[string]int, 42)
2013-04-10 14:58:50 -07:00
// set a value
x["foo"] = 3
2015-02-20 00:35:01 -05:00
// 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")
2013-04-10 14:58:50 -07:00
// make a map with a literal
2015-02-20 00:35:01 -05:00
x = map[string]int{
"foo": 2, "bar": 42, "baz": -1,
2013-04-10 14:58:50 -07:00
}