admin管理员组文章数量:1316523
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.
本文标签: goHow to use switch type on value of reflectTypeOf(value)Stack Overflow
版权声明:本文标题:go - How to use switch type on value of reflect.TypeOf(value) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742006980a2412155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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