From the course: Learning Go

Store unordered values in maps - Go Tutorial

From the course: Learning Go

Store unordered values in maps

- [Instructor] In the Go programming language, a map is an unordered collection of key value pairs. In other terms, it's a hash table that lets you store collections of data and then arbitrarily find items in the collection, based on their keys. A map's keys can be of any type that's comparable, that is the keys can be compared to each other for the purposes of sorting. But it's pretty common to use strings for keys and then any other type for associated values. I'm starting in a new version of my main.go file in my practice directory, and I'll create a new variable, and I'll initialize it with the make function. I'll parse in a type of map, then I'll set the key type wrapped in brackets to string and the associated value type also to string. Then I'll output the value of the map. And the output shows me that the map is empty, there's nothing between the brackets. Now I'll add items to the map. I'll say states, then in a pair of braces, a string based key, the first one will be WA for Washington and then the name of the state Washington. I'll duplicate that line of code a couple of times. Next one will be OR and Oregon. And the third one will be CA and California. Then I'll copy and paste my println command and I'll put the results and when you output a map object, you'll see a string representation where you see that it's a map and then the contained key value pairs are shown separated with spaces and each pair has a colon. Once the map has been created, you can retrieve an item based on its key. I'll create a new variable that I'll call California, make sure it's all lower case and I'll set its value using the syntax states, and then I'll parse in the key that I'm looking for. Then I'll use a println command and I'll put the value of the variable California. And I see the result. If you want to remove an item from a map, you can use a built-in function called delete. Parse in the name of your maps variable, and then the key you're interested in and I'll copy and paste my command to output the entire map, and I'll see that after the delete operation, Oregon is gone, but I can also add new items by once again using the syntax states and a new key, this one will be NY for New York and the associated value will be in New York. And now I see that I have California, New York and Washington. The order of display doesn't matter, and in fact, this is an unordered collection and so you can never rely on the order always being the same. Although in this example, it's in alphabetical order. If you want to iterate or loop through a map, you can use a for statement that looks like this. I'll start with for k, v . k stands for key and v stands for value. Then I'll use the range key word and say I want to loop through the states map. Each time through the loop, k will be one of the keys and v will be one of the values. I'll output that using fmt.printf and then in the quotes, I'll use a number of placeholders. The first one will be the key and the second one will be the value. I'm using the percent %v verb for both then a line feed, and then k and v to fill in those values. And now I'm looping through and outputting each item in the array. Just like the maps display order isn't guaranteed, the iteration order isn't guaranteed either. So the order of displaying I'm seeing may not match what you see on your computer. If you want to guarantee the order you have to manage it yourself. For example, maybe I want to list these States in alphabetical order. To do that, I'll extract the keys from the map as a slice of the strings array. I'll create a variable that I'll name keys, and I'll make it, and I'll parse in open and close bracket string, meaning it's a slice of string values. And then I'll set the initial value as the length of the state's variable. Now I'll add each key from the map to the slice. I'll start by creating a variable named i, I'll give it a value of zero, then I'll create a for loop, and I'll say that I'm creating a new variable named k for key each time through the loop. And then I'll say keys and I'll use the i number and set it to a value of k, and then I'll say I plus plus. And then I'll output those keys, and I see that I have a slice of string values. Now, I can sort it, as I did before with the slices, I'll come back up here and I'll add the sort package, and then I'll use the strings function from the sort package and sort that slice, and then output it again and I'll show that it's been sorted alphabetically. Now I can report all of the results. Each time I iterate through a slice using the range keyword, I'll get an integer representing the current index in the slice. So this time I'll use the syntax for i: equals range keys and in the braces, I'll output with fmt.println. Now parse in states, and in a pair of brackets, keys, and another pair of brackets, i. And so now, I'm using an iterative loop with an index number. And there's the result. I'm now outputting the state values in alphabetical order. So that's how you can use maps in Go, to store unordered data collections in memory and access the items arbitrarily using their keys. And by using slices and maps together, you can process that data in whatever order you like.

Contents