[英]golang type assertion using reflect.Typeof()
I've tried to identify a struct with string value(name). 我试图用字符串值(名称)来识别结构。
reflect.TypeOf
returns Type
. reflect.TypeOf
返回Type
。
But type assertion needs a type
. 但类型断言需要一种
type
。
How can I casting Type
to type
? 如何将
Type
为type
?
Or any suggestion to handle it? 或任何处理它的建议?
http://play.golang.org/p/3PJG3YxIyf http://play.golang.org/p/3PJG3YxIyf
package main
import (
"fmt"
"reflect"
)
type Article struct {
Id int64 `json:"id"`
Title string `json:"title",sql:"size:255"`
Content string `json:"content"`
}
func IdentifyItemType(name string) interface{} {
var item interface{}
switch name {
default:
item = Article{}
}
return item
}
func main() {
i := IdentifyItemType("name")
item := i.(Article)
fmt.Printf("Hello, item : %v\n", item)
item2 := i.(reflect.TypeOf(i)) // reflect.TypeOf(i) is not a type
fmt.Printf("Hello, item2 : %v\n", item2)
}
A type assertion, syntactically, takes a type in the parentheses, not an expression. 语法上,类型断言在括号中采用一种类型,而不是表达式。 So it is a syntax error.
所以这是一个语法错误。
You seem to be trying to do a type assertion with a value computed at runtime. 您似乎尝试使用在运行时计算的值来执行类型断言。 Does that make sense?
那有意义吗? Let's think about what a type assertion is.
让我们考虑一下类型断言是什么。
A type assertion consists of two things: 类型断言包含两件事:
x.(T)
has compile-time type T
. x.(T)
具有编译时类型T
This allows you to do stuff the expression that you can do with type T
, which you may not be able to do with the type of x
. T
可以做的表达式,你可能无法用x
的类型做。 nil
and is actually of the given type, and if not, it causes a panic. nil
并且实际上是给定类型,如果不是,则会导致混乱。 The first part obviously doesn't make sense for a type computed at runtime. 对于在运行时计算的类型,第一部分显然没有意义。 The compile-time type of the resulting expression cannot depend on something that is not known at compile-time.
结果表达式的编译时类型不能依赖于编译时未知的内容。
The second one (runtime check) can be done with a type computed at runtime. 第二个(运行时检查)可以使用在运行时计算的类型来完成。 Something like:
就像是:
if reflect.TypeOf(x) != someTypeComputedAtRuntime {
panic(42)
}
If you need to switch on the type of the outer interface{} you wont need reflection. 如果你需要打开外部接口{}的类型,你就不需要反射。
switch x.(type){
case int:
dosomething()
}
...but if you need to switch on the type of the attributes in an interface then you can do this: ...但是如果你需要在界面中打开属性的类型,那么你可以这样做:
s := reflect.ValueOf(x)
for i:=0; i<s.NumValues; i++{
switch s.Field(i).Interface().(type){
case int:
dosomething()
}
}
I haven't found a cleaner way, I'd love to know if it exists. 我没有找到更清洁的方式,我很想知道它是否存在。
I think what you're looking for here is a type switch. 我想你在这里寻找的是一种类型开关。 https://tour.golang.org/methods/16
https://tour.golang.org/methods/16
If you can handle the noise and implement an extra method which all the types implement eg 'Type() string', you can do something like this: 如果你可以处理噪音并实现一个额外的方法,所有类型实现,例如'Type()字符串',你可以这样做:
ve := &ValidationError{}
nf := &NotFound{}
switch err.Type() {
case ve.Type() :
SendBadRequest(w, err)
case nf.Type() :
http.NotFound(w, r)
default:
SendInternalError(w, err)
}
I think you can use ValueOf to solve this 我认为你可以使用ValueOf来解决这个问题
item2 := reflect.ValueOf(i)
fmt.Printf("Hello, item2 : %v\n", item2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.