Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何从任何结构类型派生结构列表-从接口{}到可变长度切片[]接口{}_Arrays_Go_Reflection_Slice - Fatal编程技术网

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

Arrays 如何从任何结构类型派生结构列表-从接口{}到可变长度切片[]接口{},arrays,go,reflection,slice,Arrays,Go,Reflection,Slice,我尝试实现一个函数,获取(任何)结构,返回这些结构的数组。ReturnArrayOfStory使用固定类型结构类型显示想法 尝试使用函数ReturnArrayOfX和reflection对任何类型执行相同操作在编译时失败 package main import ( "fmt" "reflect" ) type story_t struct { LANGUAGE string SPECIES string } func ReturnArrayOfStory(x

我尝试实现一个函数,获取(任何)结构,返回这些结构的数组。ReturnArrayOfStory使用固定类型结构类型显示想法

尝试使用函数ReturnArrayOfX和reflection对任何类型执行相同操作在编译时失败

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
主程序包
进口(
“fmt”
“反映”
)
类型故事结构{
语言字符串
种串
}
func ReturnArrayOfStory(x故事)[]故事{
x1:=x
变量a1[]故事
a1=附加(a1,x1)
a1=附加(a1,x1)
a1=附加(a1,x1)
返回a1
}
func ReturnArrayOfX(x接口{})[]接口{}{
x1:=x
v1:=reflect.ValueOf(&x1).Elem()
a1:=[]反射.类型(&x1)
//变量a1[]x
a1=附加(a1,x1)
a1=附加(a1,x1)
a1=附加(a1,x1)
//返回a1
返回a1
}
func main(){
var as1[]故事
s1:=故事{“恩”,“波斯王子”}
as1=返回阵列存储(s1)
//as1:=ReturnArrayOfX(s1)
对于i:=0;i
这是一个简化的场景。实际上,我喜欢从外部数据源(如数据库)读取大量结构类型

  • 我如何通过ReturnArrayOfX实现我的目标
  • 这可能吗?如果没有,为什么

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

第一:如果要使用反射返回类型的切片:

// 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()
}
func ReturnArrayOfX(x interface{}) []interface{} {
   ret:=make([]interface{},0)
   ret=append(ret,x)
   ret=append(ret,x)
   ret=append(ret,x)
}
您可以将其用作:

as2 := ReturnArrayOfX(s1)
arr:=as2.([]story_t)
for i := 0; i < len(arr); i++ {
    fmt.Printf("%02d %+v\n", i, arr[i])
}
然后需要处理数组的每个元素:

as2 := ReturnArrayOfX(s1)
for i := 0; i < len(as2); i++ {
    fmt.Printf("%02d %+v\n", i, as2[i])
    data:=as2[i].(story_t)
}
as2:=ReturnArrayOfX(s1)
对于i:=0;i
这里有一个通用的切片转换函数:

// 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()))
    }
}

.

感谢您的洞察力,两种解决方案都适合我,而我更喜欢第一种。好的,严格地说,我现在明白您的观点:SliceType=“[”“]”ElementType。ArrayType=“[“ArrayLength”]”ElementType。因此,我的符号按语法返回一个片段,您是对的。我相应地改了帖子的标题,现在更准确了。不过,说“可变长度切片”有点多余,因为根据定义,切片是可变长度的
// 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)