Skip to content

Instantly share code, notes, and snippets.

@computerphysicslab
Last active July 13, 2020 08:28
Show Gist options
  • Save computerphysicslab/1d42f065e6f912b53389dcc763e0f2f7 to your computer and use it in GitHub Desktop.
Save computerphysicslab/1d42f065e6f912b53389dcc763e0f2f7 to your computer and use it in GitHub Desktop.
// https://play.golang.org/p/XnuBNaigHcF
/*
Loop through an interface array to find the item matching by any of its keys/values or struct fields
In this example:
[0]:
"Fruta": "manzana",
"Verdura": "coliflor"
[1]:
"Fruta": "pera",
"Verdura": "guisante"
searching for "Fruta": "pera" yields array item[1]
OUTPUT:
#### mapas ([]main.Map) => [
{
"Fruta": "manzana",
"Verdura": "coliflor"
},
{
"Fruta": "pera",
"Verdura": "guisante"
}
]
#### Search #1 on map array (main.Map) => {
"Fruta": "manzana",
"Verdura": "coliflor"
}
#### Search #2 on map array (main.Map) => {}
#### Search #3 on map array (main.Map) => {
"Fruta": "pera",
"Verdura": "guisante"
}
#### estructuras ([]main.StructFV) => [
{
"Fruta": "manzana",
"Verdura": "coliflor"
},
{
"Fruta": "pera",
"Verdura": "guisante"
}
]
#### Search #1 on struct array (main.StructFV) => {
"Fruta": "manzana",
"Verdura": "coliflor"
}
#### Search #2 on struct array (main.StructFV) => {
"Fruta": "",
"Verdura": ""
}
#### Search #3 on struct array (main.StructFV) => {
"Fruta": "pera",
"Verdura": "guisante"
}
*/
package main
import (
"encoding/json"
"fmt"
"reflect"
)
func myDebug(s string, i interface{}) {
pretty, err := json.MarshalIndent(i, "", " ")
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("\n\n#### %s (%s) => %s\n", s, reflect.TypeOf(i).String(), string(pretty))
// fmt.Printf("myDebugPlain: %+v\n", i)
}
type Map map[string]interface{}
type StructFV struct {
Fruta string
Verdura string
}
func searchInArrayMap(_theArray interface{}, key string, value string) interface{} {
theArray := _theArray.([]Map)
for i := range theArray {
if theArray[i][key] == value {
// myDebug("searchInArrayMap found", i)
return theArray[i]
} else {
//myDebug("searchInArrayMap not found", i)
}
}
// return nil
return make(Map)
}
func searchInArrayStructFV(_theArray interface{}, key string, value string) interface{} {
theArray := _theArray.([]StructFV)
for i := range theArray {
if (key == "Fruta" && theArray[i].Fruta == value) || (key == "Verdura" && theArray[i].Verdura == value) {
// myDebug("searchInArrayStructAB found", i)
return theArray[i]
} else {
// myDebug("searchInArrayStructAB NOT found", theArray[i].A+"-"+key+":"+theArray[i].B+"-"+value)
}
}
// return nil
return StructFV{}
}
/**
* Devuelve el elemento de un array cuyo campo indicado tiene el valor tambien indicado.
*/
func searchInArray(_theArray interface{}, key string, value string) interface{} {
// myDebug("d0", "d0")
switch _theArray.(type) {
case []Map:
// myDebug("d1", "d1")
return searchInArrayMap(_theArray, key, value)
case []StructFV:
// myDebug("d2", "d2")
return searchInArrayStructFV(_theArray, key, value)
}
// myDebug("d3", "d3")
return nil
}
func main() {
// Testing search on map array
mapas := make([]Map, 2)
mapas[0] = make(Map)
mapas[0]["Fruta"] = "manzana"
mapas[0]["Verdura"] = "coliflor"
mapas[1] = make(Map)
mapas[1]["Fruta"] = "pera"
mapas[1]["Verdura"] = "guisante"
myDebug("mapas", mapas)
var result interface{}
result = searchInArray(mapas, "Fruta", "manzana")
myDebug("Search #1 on map array", result)
result = searchInArray(mapas, "Fruta", "coliflor")
myDebug("Search #2 on map array", result)
result = searchInArray(mapas, "Verdura", "guisante")
myDebug("Search #3 on map array", result)
// Testing search on struct array
var estructuras []StructFV
estructuras = append(estructuras, StructFV{"manzana", "coliflor"})
estructuras = append(estructuras, StructFV{"pera", "guisante"})
myDebug("estructuras", estructuras)
result = searchInArray(estructuras, "Fruta", "manzana")
myDebug("Search #1 on struct array", result)
result = searchInArray(estructuras, "Fruta", "coliflor")
myDebug("Search #2 on struct array", result)
result = searchInArray(estructuras, "Verdura", "guisante")
myDebug("Search #3 on struct array", result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment