【发布时间】:2020-06-11 10:14:56
【问题描述】:
我想将一些类似的 func 代码合并到一个 func 中,但是每个旧 func 都使用不同类型的结构,所以我打算通过不同类型的字符串创建模型。 所以我做这样的事情:
type A struct {
filed string
}
type B struct {
filed string
}
and still C, D, E, F here...(every struct has its own method different with others)
我想在一个地方创建这些类型:
create(typeName string) interface {
switch typeName {
case A:
return &A{}
case B:
return &B{}
....(more case than 10 times)
}
}
然后我在这里使用 create():
model := create("A")
现在model是接口类型,没有A的文件,怎么简单恢复模型类型为A
【问题讨论】:
-
这能回答你的问题吗? How to cast interface {} to struct
标签: go