To filter a map based on keys in Go, you can iterate over the map and add key-value pairs that meet the filter criteria to a new map. This method allows you to create a subset of the original map based on specific key conditions.
We can filter a map based on a range of keys in Go by iterating over the map and adding key-value pairs that fall within the specified range to a new map.
For example,
fmt
package, which provides the necessary functions for input-output operations.myMap
with some key-value pairs. In this example, the map has integer keys and string values.filteredMap
to store the filtered key-value pairs.for
loop and check if each key falls within the specified range.filteredMap
.fmt.Println
function to verify the filtering.package main
import "fmt"
func main() {
// Declare and initialize a map
myMap := map[int]string{
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
}
// Declare an empty map to store the filtered key-value pairs
filteredMap := make(map[int]string)
// Define the key range
lowerBound := 2
upperBound := 4
// Iterate over the original map and filter based on the key range
for key, value := range myMap {
if key >= lowerBound && key <= upperBound {
filteredMap[key] = value
}
}
// Print the filtered map
fmt.Println("Filtered Map by Key Range:")
for key, value := range filteredMap {
fmt.Printf("%d: %s\n", key, value)
}
}
Filtered Map by Key Range: 2: two 3: three 4: four
We can filter a map based on specific keys in Go by iterating over the map and adding key-value pairs that match the specified keys to a new map.
For example,
fmt
and sort
packages, which provide the necessary functions for input-output operations and sorting.myMap
with some key-value pairs. In this example, the map has integer keys and string values.filteredMap
to store the filtered key-value pairs.for
loop and check if each key matches one of the specified keys.filteredMap
.fmt.Println
function to verify the filtering.package main
import "fmt"
func main() {
// Declare and initialize a map
myMap := map[int]string{
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
}
// Declare an empty map to store the filtered key-value pairs
filteredMap := make(map[int]string)
// Define the specific keys to filter by
keysToFilter := []int{1, 3, 5}
// Iterate over the original map and filter based on the specific keys
for key, value := range myMap {
for _, k := range keysToFilter {
if key == k {
filteredMap[key] = value
break
}
}
}
// Print the filtered map
fmt.Println("Filtered Map by Specific Keys:")
for key, value := range filteredMap {
fmt.Printf("%d: %s\n", key, value)
}
}
Filtered Map by Specific Keys: 1: one 3: three 5: five
We can filter a map with string keys based on a specific prefix in Go by iterating over the map and adding key-value pairs that have keys starting with the given prefix to a new map.
For example,
fmt
and strings
packages, which provide the necessary functions for input-output operations and string manipulations.myMap
with some key-value pairs. In this example, the map has string keys and string values.filteredMap
to store the filtered key-value pairs.for
loop and check if each key starts with the specified prefix.filteredMap
.fmt.Println
function to verify the filtering.package main
import (
"fmt"
"strings"
)
func main() {
// Declare and initialize a map
myMap := map[string]string{
"apple": "fruit",
"banana": "fruit",
"carrot": "vegetable",
"apricot": "fruit",
"blueberry": "fruit",
}
// Declare an empty map to store the filtered key-value pairs
filteredMap := make(map[string]string)
// Define the prefix to filter by
prefix := "ap"
// Iterate over the original map and filter based on the prefix
for key, value := range myMap {
if strings.HasPrefix(key, prefix) {
filteredMap[key] = value
}
}
// Print the filtered map
fmt.Println("Filtered Map by Prefix:")
for key, value := range filteredMap {
fmt.Printf("%s: %s\n", key, value)
}
}
Filtered Map by Prefix: apple: fruit apricot: fruit
In this tutorial, we learned How to Filter a Map Based on Keys in Go language with well detailed examples.