golang 动态 实例化 结构体
2015-12-27 16:19
736 查看
真理 http://stackoverflow.com/questions/10210188/instance-new-type-golang
So, if I understand your question correctly, you are asking about how you can create an object when you just have the name of the type as string. So, for example, you might have a string "MyStruct" and you want to create an object of this type.
Unfortunately, that's not easily possible because Go is a statically typed language and the linker will eliminate dead code (or inline parts of it). So, there is no guarantee, that your final executable will even contain
the code of "MyStruct".
You can however, maintain a global
For example by initializing this map in the
of your packages which defines such discover-able types. This will also tell the compiler that you are using the types. Afterwards, you can use this map to look up the reflect.Type of
the type you want to create and use reflect.New to get a pointer to a new object of that
type (stored as a reflect.Value). You can extract the object into an interface with something like this:
Elem() will de-reference the pointer and Interface() will
return the reflected value as an
See The Laws of Reflection for further details.
PS: There might be a better way to structure your program which doesn't even require reflection and which let the compiler catch more errors. Have you considered using a factory
method for example? An other easy solution might be to maintain a
http://mikespook.com/2012/07/%E5%9C%A8-golang-%E4%B8%AD%E7%94%A8%E5%90%8D%E5%AD%97%E8%B0%83%E7%94%A8%E5%87%BD%E6%95%B0/
但这里有一个限制:这个 map 仅仅可以用原型是“func()”的没有输入参数或返回值的函数。
如果想要用这个方法实现调用不同函数原型的函数,需要用到 interface{}。
***********参考*****************
1. https://www.socketloop.com/tutorials/golang-fix-type-interface-has-no-field-or-no-methods-and-type-assertions-example
2. golang用字符串反射实例化结构体
3. http://stackoverflow.com/questions/17507697/dry-out-my-go-function-with-interfaces
4. 新手求解 golang 动态new struct
http://www.oschina.net/question/1388294_141504?sort=default&p=2#answers
5. http://stackoverflow.com/questions/13856539/how-do-you-get-struct-value-on-unknown-interface
So, if I understand your question correctly, you are asking about how you can create an object when you just have the name of the type as string. So, for example, you might have a string "MyStruct" and you want to create an object of this type.
Unfortunately, that's not easily possible because Go is a statically typed language and the linker will eliminate dead code (or inline parts of it). So, there is no guarantee, that your final executable will even contain
the code of "MyStruct".
You can however, maintain a global
map[string]reflect.Typemanually.
For example by initializing this map in the
init()function
of your packages which defines such discover-able types. This will also tell the compiler that you are using the types. Afterwards, you can use this map to look up the reflect.Type of
the type you want to create and use reflect.New to get a pointer to a new object of that
type (stored as a reflect.Value). You can extract the object into an interface with something like this:
reflect.New(yourtype).Elem().Interface()
Elem() will de-reference the pointer and Interface() will
return the reflected value as an
interface{}.
See The Laws of Reflection for further details.
PS: There might be a better way to structure your program which doesn't even require reflection and which let the compiler catch more errors. Have you considered using a factory
method for example? An other easy solution might be to maintain a
map[string]func() interface{}of functions which can be invoked to create a new object with that name.
http://mikespook.com/2012/07/%E5%9C%A8-golang-%E4%B8%AD%E7%94%A8%E5%90%8D%E5%AD%97%E8%B0%83%E7%94%A8%E5%87%BD%E6%95%B0/
但这里有一个限制:这个 map 仅仅可以用原型是“func()”的没有输入参数或返回值的函数。
如果想要用这个方法实现调用不同函数原型的函数,需要用到 interface{}。
***********参考*****************
1. https://www.socketloop.com/tutorials/golang-fix-type-interface-has-no-field-or-no-methods-and-type-assertions-example
Golang : Fix type interface{} has no field or
no methods and type assertions example
2. golang用字符串反射实例化结构体package main import ( "fmt" "reflect" ) type Foo struct { } type Bar struct { } //用于保存实例化的结构体对象 var regStruct map[string]interface{} func main() { str := "Bar" if regStruct[str] != nil { t := reflect.ValueOf(regStruct[str]).Type() v := reflect.New(t).Elem() fmt.Println(v) } } func init() { regStruct = make(map[string]interface{}) regStruct["Foo"] = Foo{} regStruct["Bar"] = Bar{} }
3. http://stackoverflow.com/questions/17507697/dry-out-my-go-function-with-interfaces
kinds := map[string]func() Entity { "user": func() Entity { return &User{} }, "space": func() Entity { return &Space{} }, "room": func() Entity { return &Room{} }, } func Create(kind string) { instance := kinds[kind]() decoder.Decode(instance) saveEntity(instance) }
4. 新手求解 golang 动态new struct
http://www.oschina.net/question/1388294_141504?sort=default&p=2#answers
<span style="font-size:14px;">我现在是这样做的 var ModelObj map[string]interface{} = map[string]interface{}{ "Member": new(Member), } func GetModObj(mod string) interface{} { if ModelObj[mod] != nil { return ModelObj[mod] } else { return nil } } </span>
5. http://stackoverflow.com/questions/13856539/how-do-you-get-struct-value-on-unknown-interface
相关文章推荐
- 关于ubuntu12.04下google-chrome无法启动问题的解决
- Go语言编程-语法
- Golang中对interface{}做type assertion和type switch
- Celery+django_celery+flask_celery+rabbitmq+flower+
- django 快速搭建blog
- Moore’s Voting Algorithm
- go爬虫:抓取股票实时股份信息发送到邮箱
- golang开发环境配置及Beego框架安装
- Django项目开发纪要
- Web 应用性能和压力测试工具 Gor
- django处理流程分析
- Delegate 代理委托 +block+类别Category
- windows下用eclipse+goclipse插件+gdb搭建go语言开发调试环境
- Google Analytics多渠道实现
- win10怎么安装Google Earth(谷歌地球)?win10安装Google Earth Pro的教程
- google大牛Jeffrey Dean
- 批处理命令——goto 和 :
- Go 1.5交叉编译
- Google Analytics基本参数配置
- Ubuntu添加新硬盘,并修改/etc/fstab自动挂载后,重启卡在开机logo处