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
Reflection 在Go结构中获取reflect.Ptr type to字段_Reflection_Go - Fatal编程技术网

Reflection 在Go结构中获取reflect.Ptr type to字段

Reflection 在Go结构中获取reflect.Ptr type to字段,reflection,go,Reflection,Go,我试图将指向结构中字段的指针的可变列表传递给第三方包。包接受一个变量接口{}列表(func Persist(…接口)错误),其中每个接口值都是指向变量的指针。我创建了一个模拟第三方库的函数,并打印出指针的类型和种类(下面称为mockFunction) 当我以非变量的方式将结构变量的地址传递给它时,它们在使用reflect调用的mock函数中具有它们的基元类型和值。但是,当我使用扩展以可变方式传递它们时,它们有Type:Type:reflect.Value和Kind:struct。第三方软件包不知

我试图将指向结构中字段的指针的可变列表传递给第三方包。包接受一个变量
接口{}
列表(
func Persist(…接口)错误
),其中每个接口值都是指向变量的指针。我创建了一个模拟第三方库的函数,并打印出指针的类型和种类(下面称为mockFunction)

当我以非变量的方式将结构变量的地址传递给它时,它们在使用reflect调用的mock函数中具有它们的基元类型和值。但是,当我使用扩展以可变方式传递它们时,它们有
Type:Type:reflect.Value
Kind:struct
。第三方软件包不知道如何以这种形式处理它们

我想找出一种方法,用接口{}(例如,
inv:=make([]接口{},3)
调用第三方包,如果可能的话,在调用
Persist(inv…)
时使用变量展开

下面是代码,其中包含一个前往游乐场的链接:

package main

import (
  "fmt"
  "reflect"
)

type Investment struct {
  Price  float64
  Symbol string
  Rating int64
}

func main() {
  inv := Investment{Price: 534.432, Symbol: "GBG", Rating: 4}
  s := reflect.ValueOf(&inv).Elem()
  variableParms := make([]interface{}, s.NumField())
  for i := 0; i < s.NumField(); i++ {
    variableParms[i] = s.Field(i).Addr()
  }

  // non-variadic call
  mockFunction(&inv.Price, &inv.Symbol, &inv.Rating)
  //variadic call
  mockFunction(variableParms...)
}

func mockFunction(values ...interface{}) {
  for i, value := range values {
    rv := reflect.ValueOf(value)
    fmt.Printf("value %d has Type: %s and Kind %s\n", i, rv.Type(), rv.Kind())
  }
}
当我使用可变参数运行它时,值是不同的,第三方软件包不知道如何处理这些类型:

value 0 has Type: reflect.Value and Kind struct
value 1 has Type: reflect.Value and Kind struct
value 2 has Type: reflect.Value and Kind struct
是否有任何方法来构造切片定义和对切片中放置内容的调用,以便可以对其进行可变扩展,并且看起来像是以非可变方式将指针传递给结构字段?

返回字段指针的反射。调用值以获取实际指针作为接口{}

variableParms[i] = s.Field(i).Addr().Ptr()

我认为,自2014年以来,Go对这起案件的处理方式可能发生了变化-当然,上面的代码在Go 1.10中不再适用于我

但是,下面的代码可以为我创建一个合适的
[]接口{}
,以所描述的方式使用

func settableSliceFromStruct(inStruct interface{}) ([]interface{}, error) {
    t := reflect.TypeOf(inStruct)
    if t.Kind() != reflect.Ptr {
        return nil, errors.New("can only assign values with pointer to struct")
    }
    v := reflect.ValueOf(inStruct).Elem()
    t = t.Elem()

    dataColumns := make([]interface{}, 0, t.NumField())
    for i := 0; i < t.NumField(); i++ {
        if weWantToIncludeThis(t.Field(i)) {
            dataColumns = append(dataColumns, v.Field(i).Addr().Interface())
        }
    }
    return dataColumns, nil
}
func settableSliceFromStruct(inStruct interface{}) ([]interface{}, error) {
    t := reflect.TypeOf(inStruct)
    if t.Kind() != reflect.Ptr {
        return nil, errors.New("can only assign values with pointer to struct")
    }
    v := reflect.ValueOf(inStruct).Elem()
    t = t.Elem()

    dataColumns := make([]interface{}, 0, t.NumField())
    for i := 0; i < t.NumField(); i++ {
        if weWantToIncludeThis(t.Field(i)) {
            dataColumns = append(dataColumns, v.Field(i).Addr().Interface())
        }
    }
    return dataColumns, nil
}
variableParms[i] = s.Field(i).Addr().Interface()