Home
Go
Convert Map to Slice
This page was last reviewed on Feb 23, 2023.
Dot Net Perls
Convert map, slice. In Go a map is ideal for fast lookups—we can find, add and remove elements with great speed. But it is not good for iterating (looping) over elements.
Shows a map
Slice forms. We sometimes want a slice of keys, values, or pairs. And a "flat slice" one where all the keys and values are stored together one after another is also helpful.
map
Slice
Example program. Here we begin with a map of string keys and string values. We then show how to get just the keys, or just the values, in a separate string slice.
Info We get the keys from the map by using "range" and appending to an empty slice with append().
Next We get the values from the map with range. We ignore the key in each iteration as we do not need it.
Shows a map
package main import "fmt" func main() { // Create example map. m := map[string]string{ "java": "coffee", "go": "verb", "ruby": "gemstone", } // Convert map to slice of keys. keys := []string{} for key, _ := range m { keys = append(keys, key) } // Convert map to slice of values. values := []string{} for _, value := range m { values = append(values, value) } // Print the results. fmt.Println("MAP ", m) fmt.Println("KEYS SLICE ", keys) fmt.Println("VALUES SLICE", values) }
MAP map[go:verb java:coffee ruby:gemstone] KEYS SLICE [java go ruby] VALUES SLICE [coffee verb gemstone]
Get pairs. Sometimes in Golang programs we want a slice of 2-element string arrays from our map. We can create these in a slice of pairs.
Also We can get a flat slice of the pairs. We place all the keys and values one after another in a flattened slice.
package main import "fmt" func main() { m := map[string]string{ "java": "coffee", "go": "verb", "ruby": "gemstone", } // Convert map to slice of key-value pairs. pairs := [][]string{} for key, value := range m { pairs = append(pairs, []string{key, value}) } // Convert map to flattened slice of keys and values. flat := []string{} for key, value := range m { flat = append(flat, key) flat = append(flat, value) } // Results. fmt.Println("MAP ", m) fmt.Println("PAIRS SLICE ", pairs) fmt.Println("FLAT SLICE ", flat) }
MAP map[go:verb java:coffee ruby:gemstone] PAIRS SLICE [[java coffee] [go verb] [ruby gemstone]] FLAT SLICE [java coffee go verb ruby gemstone]
Some notes. We can extract data from a map by using for-range loops. When a map is iterated over in this kind of loop, we access each key and value.
for
range
A summary. Often constructs like slices that are "flattened" contain all keys and values together are useful in real programs. They may also be simpler to use.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 23, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.