获取结构体的反射类型。

huangapple go评论86阅读模式
英文:

Getting reflect.Type of structure

问题

在Go语言中,可以通过结构本身来检索reflect.Type吗?

伪代码:

type MyStruct struct {
  Name string
}

t := reflect.TypeOf(MyStruct)

然后,是否可以随后创建该类型的切片?

更新:
我知道针对这个问题的解决方案是reflect.TypeOf((*t1)(nil)).Elem()。但我正在寻找更好的解决方案,因为这个解决方案对我来说似乎不太友好。我将尝试解释一下情况。

在开发数据库模型上面的“通用”数据服务时,我想要做以下操作:

ds := NewDataService(db.Collection("MyStruct"), MyStruct)

其中DataService能够使用该模型执行Find、Insert等操作。因此,我需要传递该结构,以便可以正确使用该模型(例如在HTTP服务器中)。

第二部分需要创建一个包含找到的对象的切片,因为我正在使用Mongo,所以在db.Collection中没有类似schema的东西可用。

英文:

Is it possible in Go to retrieve reflect.Type from structure itself?

pseudo:

type MyStruct struct {
  Name string
}

type := reflect.TypeOf(MyStruct)

And is it possible to make slice of that type afterwards?

Update:
I am aware of reflect.TypeOf((*t1)(nil)).Elem() solution for this problem. I am looking for a better solution to this, as this seems to me pretty unfriendly. I'll try to explain the situation.

While developing 'generic' dataservice above database model, I want to do something like:

ds := NewDataService(db.Collection("MyStruct"), MyStruct)

where DataService is able to do Find, Insert etc. using that model. Therefore I need to pass the structure so the model can be used correctly (for example with http server).

The second part is needed as Find should return slice of found objects.

Because I am using Mongo, there is nothing like schema available within db.Collection

答案1

得分: 1

第一部分:这是一个重复的问题,可以在https://stackoverflow.com/questions/6390585/in-golang-is-is-possible-get-reflect-type-from-the-type-itself-from-name-as-st找到答案。

第二部分:之后可以创建该类型的切片:

你可以使用Type.SliceOf()获取已有类型元素为切片的Type,然后可以使用reflect.MakeSlice()函数创建该类型的切片。它返回一个Value,你可以使用其Value.Interface()方法获取一个interface{},如果你需要将结果作为[]MyStruct类型,可以使用类型断言

tt := reflect.TypeOf((*MyStruct)(nil)).Elem()
fmt.Println(tt)

ms := reflect.MakeSlice(reflect.SliceOf(tt), 10, 20).Interface().([]MyStruct)
ms[0].Name="test"
fmt.Println(ms)

输出结果(<kbd>Go Playground</kbd>):

main.MyStruct
[{test} {} {} {} {} {} {} {} {} {}]
英文:

For the first part: it is a duplicate of https://stackoverflow.com/questions/6390585/in-golang-is-is-possible-get-reflect-type-from-the-type-itself-from-name-as-st

For the second part: make slice of that type afterwards:

You can get the Type of the slice whose elements' type is what you already have by using Type.SliceOf(), and you can use the reflect.MakeSlice() function to create a slice of such type. It returns a Value, you can use its Value.Interface() method to obtain an interface{} on which you can use type assertion if you need the result as a type of []MyStruct:

tt := reflect.TypeOf((*MyStruct)(nil)).Elem()
fmt.Println(tt)

ms := reflect.MakeSlice(reflect.SliceOf(tt), 10, 20).Interface().([]MyStruct)
ms[0].Name=&quot;test&quot;
fmt.Println(ms)

Output (<kbd>Go Playground</kbd>):

main.MyStruct
[{test} {} {} {} {} {} {} {} {} {}]

huangapple
  • 本文由 发表于 2015年6月26日 21:34:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/31074609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定