Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了golang 动态 实例化 结构体大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。


真理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@H_978_23@map[String]reflect.Type@H_412_24@manually. For example by initializing this map in theinit()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 thereflect.Typeof the type you want to create and usereflect.Newto 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 andInterface()will return the reflected value as aninterface{}. SeeThe Laws of Reflectionfor 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 afactory methodfor example? An other easy solution might be to maintain a@H_961_25@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 return&User{}},"space"Space"room"Room}   func Createkind ) instance  kinds[kind]() decoderDecodeinstance saveEntity}

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

大佬总结

以上是大佬教程为你收集整理的golang 动态 实例化 结构体全部内容,希望文章能够帮你解决golang 动态 实例化 结构体所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。