Skip to content

Instantly share code, notes, and snippets.

@PumpkinSeed
Created September 22, 2017 12:26
Show Gist options
  • Save PumpkinSeed/aed0881859a41c88a4e9aa012cfe6fb1 to your computer and use it in GitHub Desktop.
Save PumpkinSeed/aed0881859a41c88a4e9aa012cfe6fb1 to your computer and use it in GitHub Desktop.
func Contains(s, value interface{}) bool {
v := reflect.ValueOf(s)
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
typeOfField := field.Type()
switch typeOfField {
case stringType:
if containsString(field.String(), value) {
return true
}
case intType, int8Type, int16Type, int32Type, int64Type:
if containsInt(field.Int(), value) {
return true
}
case boolType:
if containsBool(field.Bool(), value) {
return true
}
case float32Type:
if containsFloat32(field.Interface().(float32), value) {
return true
}
case float64Type:
if containsFloat64(field.Interface().(float64), value) {
return true
}
case complex64Type:
if containsComplex64(field.Interface().(complex64), value) {
return true
}
case complex128Type:
if containsComplex128(field.Interface().(complex128), value) {
return true
}
}
}
return false
}
/*
Contains helper functions
*/
func containsString(s string, v interface{}) bool {
switch v.(type) {
case string:
if v.(string) == s {
return true
}
}
return false
}
func containsInt(s int64, v interface{}) bool {
switch v.(type) {
case int64:
if v.(int64) == s {
return true
}
case int32:
if int64(v.(int32)) == s {
return true
}
case int16:
if int64(v.(int16)) == s {
return true
}
case int8:
if int64(v.(int8)) == s {
return true
}
case int:
if int64(v.(int)) == s {
return true
}
}
return false
}
func containsBool(s bool, v interface{}) bool {
switch v.(type) {
case bool:
if v.(bool) == s {
return true
}
}
return false
}
func containsFloat32(s float32, v interface{}) bool {
switch v.(type) {
case float32:
if v.(float32) == s {
return true
}
}
return false
}
func containsFloat64(s float64, v interface{}) bool {
switch v.(type) {
case float64:
if v.(float64) == s {
return true
}
}
return false
}
func containsComplex64(s complex64, v interface{}) bool {
switch v.(type) {
case complex64:
if v.(complex64) == s {
return true
}
}
return false
}
func containsComplex128(s complex128, v interface{}) bool {
switch v.(type) {
case complex128:
if v.(complex128) == s {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment