To convert an array of key-value pairs to a map in Go, you can iterate over the array and insert each key-value pair into the map. This method allows you to create a map from a collection of key-value pairs stored in an array.
We can convert a slice of key-value pairs to a map in Go by iterating over the slice and adding each key-value pair to the map.
For example,
pairs
with some key-value pairs. In this example, the pairs are represented as structs with two fields: Key
and Value
.myMap
to store the key-value pairs.for
loop and add each pair to the map.fmt.Println
function to verify the conversion.package main
import "fmt"
// Pair represents a key-value pair
type Pair struct {
Key int
Value int
}
func main() {
// Declare and initialize a slice of pairs
pairs := []Pair{
{1, 10},
{2, 20},
{3, 30},
{4, 40},
{5, 50},
}
// Declare an empty map to store the key-value pairs
myMap := make(map[int]int)
// Iterate over the slice and add each pair to the map
for _, pair := range pairs {
myMap[pair.Key] = pair.Value
}
// Print the resulting map
fmt.Println("Map from slice of pairs:")
for key, value := range myMap {
fmt.Printf("%d: %d\n", key, value)
}
}
Map from slice of pairs: 1: 10 2: 20 3: 30 4: 40 5: 50
We can convert a slice of key-value tuples to a map in Go by iterating over the slice and adding each tuple's key-value pair to the map.
For example,
pairs
with some key-value pairs. In this example, the tuples are represented as slices with two elements: a key and a value.myMap
to store the key-value pairs.for
loop and add each tuple to the map.fmt.Println
function to verify the conversion.package main
import "fmt"
func main() {
// Declare and initialize a slice of tuples
pairs := [][2]int{
{1, 10},
{2, 20},
{3, 30},
{4, 40},
{5, 50},
}
// Declare an empty map to store the key-value pairs
myMap := make(map[int]int)
// Iterate over the slice and add each tuple to the map
for _, pair := range pairs {
myMap[pair[0]] = pair[1]
}
// Print the resulting map
fmt.Println("Map from slice of tuples:")
for key, value := range myMap {
fmt.Printf("%d: %d\n", key, value)
}
}
Map from slice of tuples: 1: 10 2: 20 3: 30 4: 40 5: 50
We can convert a slice of structs with key-value fields to a map in Go by iterating over the slice and adding each struct's key-value pair to the map.
For example,
pairs
with some key-value pairs. Each struct has two fields: Key
and Value
.myMap
to store the key-value pairs.for
loop and add each struct's key-value pair to the map.fmt.Println
function to verify the conversion.package main
import "fmt"
// Pair represents a key-value pair
type Pair struct {
Key int
Value int
}
func main() {
// Declare and initialize a slice of structs
pairs := []Pair{
{1, 10},
{2, 20},
{3, 30},
{4, 40},
{5, 50},
}
// Declare an empty map to store the key-value pairs
myMap := make(map[int]int)
// Iterate over the slice and add each struct's key-value pair to the map
for _, pair := range pairs {
myMap[pair.Key] = pair.Value
}
// Print the resulting map
fmt.Println("Map from slice of structs:")
for key, value := range myMap {
fmt.Printf("%d: %d\n", key, value)
}
}
Map from slice of structs: 1: 10 2: 20 3: 30 4: 40 5: 50
In this tutorial, we learned How to Convert an Array of Key-Value Pairs to a Map in Go language with well detailed examples.