如何从任何结构类型派生结构列表-从interface {}到可变长度切片[] interface {}

问题描述 投票:-1回答:2

我尝试实现采用(任意)结构的函数,并返回这些结构的数组。 ReturnArrayOfStory用固定类型的struct类型显示了这个想法。

尝试对具有ReturnArrayOfX函数的任何类型执行相同操作,并且在编译时反射失败。

package main

import (
    "fmt"
    "reflect"
)
type story_t struct {
    LANGUAGE string
    SPECIES  string
}

func ReturnArrayOfStory(x story_t) []story_t {
    x1 := x
    var a1 []story_t
    a1 = append(a1, x1)
    a1 = append(a1, x1)
    a1 = append(a1, x1)
    return a1
}

func ReturnArrayOfX(x interface{}) []interface{} {
    x1 := x
    v1 := reflect.ValueOf(&x1).Elem()
    a1 := []reflect.TypeOf(&x1)
    //  var a1 []x
    a1 = append(a1, x1)
    a1 = append(a1, x1)
    a1 = append(a1, x1)
    //return a1
    return a1
}

func main() {

    var as1 []story_t

    s1 := story_t{"EN", "Prince of Persia"}

    as1 = ReturnArrayOfStory(s1)
    //as1 := ReturnArrayOfX(s1)
    for i := 0; i < len(as1); i++ {
        fmt.Printf("%02d %+v\n", i, as1[i])
    }

    as2 := ReturnArrayOfX(s1)
    //as1 := ReturnArrayOfX(s1)
    for i := 0; i < len(as2); i++ {
        fmt.Printf("%02d %+v\n", i, as2[i])
    }

}

a1 := []reflect.TypeOf(&x1)
main.go:25:8: reflect.TypeOf is not a type

这是一个简化的方案。实际上,我喜欢从外部数据源(如数据库)中读取大量结构类型。

  • 如何使用ReturnArrayOfX实现我的目标?
  • 列表项可能吗?如果不是,为什么
arrays go reflection slice
2个回答
1
投票

您的问题有两种解决方案:

首先:如果要使用反射返回类型的切片:

// You cannot return []interface{}, because this function will return [](type of x), and that is not []interface{}
func ReturnArrayOfX(x interface{}) interface{} {
    x1 := x
    a1 := 
// this creates *[](typeOf x)
reflect.New(reflect.SliceOf(reflect.TypeOf(x)))
// Append the first element to *[](typeof x)
// after this, a1 now points to a slice, not to a slice *
    a1 = reflect.Append(a1.Elem(), reflect.ValueOf(x1))
    a1 = reflect.Append(a1, reflect.ValueOf(x1))
    a1 = reflect.Append(a1, reflect.ValueOf(x1))
    //return [](typeof x)
    return a1.Interface()
}

您可以将其用作:

as2 := ReturnArrayOfX(s1)
arr:=as2.([]story_t)
for i := 0; i < len(arr); i++ {
    fmt.Printf("%02d %+v\n", i, arr[i])
}

第二:您可以返回[] interface {}而不进行反射:

func ReturnArrayOfX(x interface{}) []interface{} {
   ret:=make([]interface{},0)
   ret=append(ret,x)
   ret=append(ret,x)
   ret=append(ret,x)
}

然后您需要处理数组的每个元素:

as2 := ReturnArrayOfX(s1)
for i := 0; i < len(as2); i++ {
    fmt.Printf("%02d %+v\n", i, as2[i])
    data:=as2[i].(story_t)
}

0
投票

这是通用切片转换功能:

// convertSlice copies the slice in src to the slice pointed to by pdst. 
// The concrete values in src must be assignable to the dst elements.
func convertSlice(pdst interface{}, src interface{}) {
    dstv := reflect.ValueOf(pdst).Elem()
    srcv := reflect.ValueOf(src)
    dstv.Set(reflect.MakeSlice(dstv.Type(), srcv.Len(), srcv.Len()))
    for i := 0; i < srcv.Len(); i++ {
        dstv.Index(i).Set(reflect.ValueOf(srcv.Index(i).Interface()))
    }
}

像这样使用它:

// Convert []story_t to []interface{}

s0 := []story_t{{"EN", "Prince of Persia"}, {"EN", "Karateka"}}
var s1 []interface{}
convertSlice(&s1, s0)

// Convert []interface{} containing story_t to []story_t

var s2 []story_t
convertSlice(&s2, s1)

Run it on the playground

© www.soinside.com 2019 - 2024. All rights reserved.