Does any one know if in Go I we have something like is ?
I need to understand if a var. is map or interface.
In Python I can do smt like this:
map = list()
print(map is list())
if map is list():
# do something
In Go I tried to do the same:
var name map[string]string{
"name": "surname"
}
var surname []interface{}
fmt.Println(reflect.TypeOf(name) map[string]string{})
fmt.Println(reflect.TypeOf(surname) []interface{})
if name == map[string]string{}{
//do something
}
Thanks in advance!
>Solution :
Use a type assertion to assert that an interface value contains a concrete value of a type:
var v interface{} = /* some value */
if v, ok := v.(map[string]string); ok{
// v has type map[string]string
}
Use a type switch when checking several types:
switch v := v.(type) {
case string:
// v has type string
case map[string]string:
// v has type map[string]string
default:
panic(fmt.Sprintf("type %T not handled", v))
}