反射。将结构体的切片值设置为一个结构体,不要使用类型断言(因为它是未知的) [英] reflect.Set slice-of-structs value to a struct, without type assertion (because it's unknown)

查看:232
本文介绍了反射。将结构体的切片值设置为一个结构体,不要使用类型断言(因为它是未知的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个帮助程序包来从队列中弹出有效载荷。这个帮助程序对导入它的应用程序使用的结构是不可知的。



这个(no-op,就是例子)函数将从队列中提供一个单一的有效载荷,提供的类型 like interface {}

  func One like interface {}} interface {} {
typ:= reflect.TypeOf(like)
one:= reflect.New(typ)

return one.Interface()





这个函数提供了很多有效载荷:

  func Many(num int,like interface {})interface {} {
typ:= reflect.TypeOf(like)
many:= reflect。 MakeSlice(reflect.SliceOf(typ),num,num)

for i:= 0;我< NUM; i ++ {
one:= One(typ)
many.Index(i).Set(one)
}

return many.Interface()
}

使用示例如下:

< pre $ type Payload struct {
Id int
Text string
}

func main(){
Many (4,Payload {})
}

然而,上面的结果是: p>

  panic:reflect.Set:类型的值** reflect.rtype不可分配给main.Payload 

https ://play.golang.org/p/ud23ZlD3Bx

解决方案

您打电话给 reflect.TypeOf reflect.Value 上,这是 ** reflect.rtype 来自 $ b

通过调用一个函数就像值,并将该结果分配给切片。

  func一个(如界面{})界面{} {
typ:= reflect.TypeOf(如)
one:= reflect。新(典型)

返回one.Interface()
}

func很多(num int,类似于interface {}} interface {} {
typ:= reflect.TypeOf(like)
many:= reflect.MakeSlice(reflect.SliceOf(typ),num,num)

for i:= 0;我< NUM; i ++ {
one:= One(like)
many.Index(i).Set(reflect.ValueOf(one).Elem())
}

返回many.Interface()
}

https://play.golang.org/p/fHF_zrymcI


I'm creating a helper package to pop payloads from a queue. It's essential that this helper be agnostic to the struct used by the application importing it.

This (no-op, just example) function will provide a single payload from the queue, of the provided type like interface{}:

func One(like interface{}) interface{} {
    typ := reflect.TypeOf(like)
    one := reflect.New(typ)

    return one.Interface()
}

This function provides many payloads:

func Many(num int, like interface{}) interface{} {
    typ := reflect.TypeOf(like)
    many := reflect.MakeSlice(reflect.SliceOf(typ), num, num)

    for i := 0; i < num; i++ {
        one := One(typ)
        many.Index(i).Set(one)
    }

    return many.Interface()
}

An example of usage is:

type Payload struct {
    Id int
    Text string
}

func main() {
    Many(4, Payload{})
}

However, the above results in:

panic: reflect.Set: value of type **reflect.rtype is not assignable to type main.Payload

https://play.golang.org/p/ud23ZlD3Bx

解决方案

You're calling reflect.TypeOf on a reflect.Value, which is where **reflect.rtype is coming from.

Call your One function wth the like value directly, and assign that result to the slice.

func One(like interface{}) interface{} {
    typ := reflect.TypeOf(like)
    one := reflect.New(typ)

    return one.Interface()
}

func Many(num int, like interface{}) interface{} {
    typ := reflect.TypeOf(like)
    many := reflect.MakeSlice(reflect.SliceOf(typ), num, num)

    for i := 0; i < num; i++ {
        one := One(like)
        many.Index(i).Set(reflect.ValueOf(one).Elem())
    }

    return many.Interface()
}

https://play.golang.org/p/fHF_zrymcI

这篇关于反射。将结构体的切片值设置为一个结构体,不要使用类型断言(因为它是未知的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆