In Go, map
is a built-in data type that maps keys to values. In other programming languages, you might be familiar with the concept of map
as a dictionary, hash table, key/value store or an associative array.
Syntactically, map
looks like this:
map[KeyType]ElementType
It is also important to know that each key is unique, meaning that assigning the same key twice will overwrite the value of the corresponding key.
To create a map, you can do:
// With map literal
foo := map[string]int{}
or
// or with make function
foo := make(map[string]int)
Here are some operations that you can do with a map
// Add a value in a map with the `=` operator:
foo["bar"] = 42
// Here we update the element of `bar`
foo["bar"] = 73
// To retrieve a map value, you can use
baz := foo["bar"]
// To delete an item from a map, you can use
delete(foo, "bar")
If you try to retrieve the value for a key which does not exist in the map, it will return the zero value of the value type.
This can confuse you, especially if the default value of your ElementType
(for example, 0 for an int), is a valid value.
To check whether a key exists in your map, you can use
value, exists := foo["baz"]
// If the key "baz" does not exist,
// value: 0; exists: false
A friend of yours has an old wholesale store called Gross Store. The name comes from the quantity of the item that the store sell: it's all in gross unit. Your friend asked you to implement a point of sale (POS) system for his store. First, you want to build a prototype for it. In your prototype, your system will only record the quantity. Your friend gave you a list of measurements to help you:
Unit | Score |
---|---|
quarter_of_a_dozen | 3 |
half_of_a_dozen | 6 |
dozen | 12 |
small_gross | 120 |
gross | 144 |
great_gross | 1728 |
In order to use the measurement, you need to store the measurement in your program.
units := Units()
fmt.Println(units)
// Output: map[...] with entries like ("dozen": 12)
You need to implement a function that create a new (empty) bill for the customer.
bill := NewBill()
fmt.Println(bill)
// Output: map[]
To implement this, you'll need to:
false
if the given unit
is not in the units
map.bill
, indexed by the item name, then return true
.unit
.bill := NewBill()
units := Units()
ok := AddItem(bill, units, "carrot", "dozen")
fmt.Println(ok)
// Output: true (since dozen is a valid unit)
Note that the returned value is type
bool
.
To implement this, you'll need to:
false
if the given item is not in the billfalse
if the given unit
is not in the units
map.false
if the new quantity would be less than 0.bill
then return true
.true
.bill := NewBill()
units := Units()
ok := RemoveItem(bill, units, "carrot", "dozen")
fmt.Println(ok)
// Output: false (because there are no carrots in the bill)
Note that the returned value is type
bool
.
To implement this, you'll need to:
0
and false
if the item
is not in the bill.bill
and true
.bill := map[string]int{"carrot": 12, "grapes": 3}
qty, ok := GetItem(bill, "carrot")
fmt.Println(qty)
// Output: 12
fmt.Println(ok)
// Output: true
Note that the returned value are types
int
andbool
.
Sign up to Exercism to learn and master Go with 34 concepts, 141 exercises, and real human mentoring, all for free.