How is it possible to use a switch type on the value of reflect.TypeOf(value)
import (
"fmt"
"reflect"
)
func main() {
var value int = 123
t := reflect.TypeOf(value)
switch tv := t.(type) {
case int:
fmt.Println("type: INT")
}
fmt.Println(t)
}
How is it possible to use a switch type on the value of reflect.TypeOf(value)
import (
"fmt"
"reflect"
)
func main() {
var value int = 123
t := reflect.TypeOf(value)
switch tv := t.(type) {
case int:
fmt.Println("type: INT")
}
fmt.Println(t)
}
Share
Improve this question
asked Jan 29 at 9:22
clarkkclarkk
1
4
|
3 Answers
Reset to default 0t
's type is the reflect.Type
interface type, and the concrete type stored in it is always the unexported *reflect.rtype
pointer type descriptor, so there's no sense type-switching on that.
Use a switch
statement and compare to other reflect.Type
values like this:
for _, v := range []any{
int(123), "foo", float64(1.0),
} {
t := reflect.TypeOf(v)
switch t {
case reflect.TypeOf(int(0)):
fmt.Println("type: INT")
case reflect.TypeOf(""):
fmt.Println("type: STRING")
default:
fmt.Println("type: other:", t)
}
}
This will output (try it on the Go Playground):
type: INT
type: STRING
type: other: float64
If you have to run this many times, you can cache the type descriptors you're interested in:
var (
typeInt = reflect.TypeOf(int(0))
typeString = reflect.TypeOf("")
)
Then the switch
:
switch t {
case typeInt:
fmt.Println("type: INT")
case typeString:
fmt.Println("type: STRING")
default:
fmt.Println("type: other:", t)
}
This outputs the same. Try this one on the Go Playground.
You might consider switching on the reflect.Kind
instead of reflect.Type
, to me this code is more readable and easier to maintain as well defined constants can be used instead of creating extra vars in memory.
Go Playground example
package main
import (
"fmt"
"reflect"
)
type namedStruct struct{}
func main() {
for _, v := range []any{
123, "foo", 1.0, struct{}{}, namedStruct{}, &namedStruct{},
} {
t := reflect.TypeOf(v)
switch k := t.Kind(); k {
case reflect.Int:
fmt.Println(k.String())
case reflect.String:
fmt.Println(k.String())
case reflect.Float64, reflect.Float32:
fmt.Println(k.String())
default:
fmt.Println("other:", k.String())
}
}
}
This prints:
int
string
float64
other: struct
other: struct
other: ptr
Instead of a type switch, use t.Kind() to determine the type.
package main
import (
"fmt"
"reflect"
)
func main() {
var value int = 123
t := reflect.TypeOf(value)
switch t.Kind() {
case reflect.Int:
fmt.Println("type: INT")
case reflect.String:
fmt.Println("type: STRING")
default:
fmt.Println("type: UNKNOWN")
}
fmt.Println("Reflect Type:", t)
}
reflect.Type.Kind()
returns areflect.Kind
value that represents the underlying type (e.g.,reflect.Int
,reflect.String
).switch
can be used ont.Kind()
to determine the type.
reflect.Type
– Mr_Pink Commented Jan 29 at 9:26reflect.Type
, but that almost always comes from areflect.Value
, in which case you would still use a regular type switch. – Mr_Pink Commented Jan 29 at 14:19