Skip to content

Instantly share code, notes, and snippets.

@fallenhitokiri
Created December 2, 2013 20:19
Show Gist options
  • Save fallenhitokiri/7758073 to your computer and use it in GitHub Desktop.
Save fallenhitokiri/7758073 to your computer and use it in GitHub Desktop.
func filterEntities(s []*Entity, key string, val string) (res []*Entity) {
val = strings.ToLower(val)
for _, e := range s {
switch reflect.ValueOf(e).Elem().FieldByName(key).Kind() {
case reflect.Slice:
imm := reflect.ValueOf(e).Elem().FieldByName(key)
for i := 0; i < imm.Len(); i++ {
v := imm.Index(i).String()
if strings.ToLower(v) == val {
res = append(res, e)
}
}
case reflect.String:
v := reflect.ValueOf(e).Elem().FieldByName(key).String()
if strings.ToLower(v) == val {
res = append(res, e)
}
}
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment