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]