Dear all,
For a given variable, say x, a type switch can be used
to execute different commands depending on the
type of x as follows:
switch value := x.(type) {
case string:
...
case session.CsrfToken:
...
default:
...
}
Now I came across a situation where the variable is
not given directly but instead is found via the introspection
facilities in the reflect module:
var valueOfX reflect.Value
...
typeOfX := x.Type()
I would like to use a type switch for this variable.
The best I came up with so far was
switch valueOfX.Interface().(type) {
...
}
Is there a better way to do this? What if I only have
typeOfX? For this case I could only think of
switch reflect.New(typeOfX).Elem().Interface().(type) {
...
}
which looks clumpsy! Any better way of doing this?
Many thanks,
Jochen