I have an array of a struct and a maps with variable name and some filter values. I want to filter my array with my maps.
Example Go Playground:
package main
import "fmt"
type cnts []cnt
type cnt struct {
ID int `json:"Id"`
Area string `json:"Area"`
State string `json:"State"`
City string `json:"City"`
}
func main() {
mycnts := cnts{
cnt{124, "Here", "South", "Home"},
cnt{125, "Here", "West", "Home"},
cnt{126, "", "South", "Home"},
cnt{127, "Here", "West", "NY"}}
// my maps with filter
mapFilter := map[string]string{"Area": "Here", "City": "Home"}
fmt.Println(mapFilter)
mycntsFilter := make(cnts, 0)
for _, val := range mycnts {
// I want to select only row where the map filter it's ok
mycntsFilter = append(mycntsFilter, val)
fmt.Println(val, mycntsFilter)
}
}
What is the best way to filter my data with dynamic filter (Represente here by a map of string)?