How to compare map[string]interface{} 's value string or not
m3 := map[string]interface{}{
"something":1,
"brawba":"Bawrbawr",
}
for key, value := range m3{
if(reflect.TypeOf(value) == string or not){
... // here
}else{
...
}
}
Use a type assertion to determine if the value is a string
:
for key, value := range m3 {
if s, ok := value.(string); ok {
fmt.Printf("%s is the string %q\n", key, s)
} else {
fmt.Printf("%s is not a string\n", key)
}
}
Use reflect to determine if the value's base type string:
type mystring string
m3 := map[string]interface{}{
"something": 1,
"brawba": "Bawrbawr",
"foo": mystring("bar"),
}
for key, value := range m3 {
if reflect.ValueOf(value).Kind() == reflect.String {
fmt.Printf("%s is a string with type %T and value %q\n", key, value, value)
} else {
fmt.Printf("%s is not a string\n", key)
}
}