From the course: Learning Go

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Store unordered values in maps

Store unordered values in maps - Go Tutorial

From the course: Learning Go

Store unordered values in maps

- [Instructor] In Go as in many other languages, a map is an unordered collection of key-value pairs. It's essentially a hash table that lets you store collections of data and then arbitrarily find items in the collections by their keys. A map's keys can be of any type that's comparable for the purposes of sorting, but it's common to use strings for keys and then any other type for the associated values. Just as with slices, you should use the built-in make function to create a map. I'll create a new map that I'll call states, and I'll assign the value using the make function, and to do that, I'll use the map type. After the type you put in the type of the key in brackets. My keys will be strings and the associated values will also be strings. Then I'll output the states map. The output indicates that the map is empty. Now, I'll add items to the map. To create the new item in the map, start with the key, and again, that's going to be a string in this version. I'll use WA for the key…

Contents