TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang Convert Map to Slice

Convert a map to a slice of keys, a slice of values, a slice of pairs and a flattened slice.
Convert map, slice. 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.mapSlice
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.
Example program. Consider this program. We begin with a map of string keys and string values—it contains some common vocabulary words.

Keys: We get the keys from the map by using "range" and appending to an empty slice with append().

Values: Next we get the values from the map with range. We ignore the key in each iteration as we do not need it.

Pairs: Suppose we want a slice of 2-element string arrays. We can create these in a slice of pairs.

Flat: Here we place all the keys and values one after another in a flattened slice. This is easy to loop over.

Golang program that converts map to slices 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) } // 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) } // Print the results. fmt.Println("MAP ", m) fmt.Println("KEYS SLICE ", keys) fmt.Println("VALUES SLICE", values) fmt.Println("PAIRS SLICE ", pairs) fmt.Println("FLAT SLICE ", flat) } Output MAP map[go:verb ruby:gemstone java:coffee] KEYS SLICE [java go ruby] VALUES SLICE [verb gemstone coffee] PAIRS SLICE [[ruby gemstone] [java coffee] [go verb]] 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.Forrange
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf