在Golang类型断言中使用反射数组类型是可能的吗?

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

Is it possible to use reflected array type in Golang type assertions?

问题

我需要检查interface{}是否为数组,并在是数组时创建相应的切片。不幸的是,我事先不知道数组的长度。

例如:

import (
  "reflect"
)

func AnythingToSlice(a interface{}) []interface{} {
  rt := reflect.TypeOf(a)
  switch rt.Kind() {
  case reflect.Slice:
    slice, ok := a.([]interface{})
    if ok {
      return slice
    }
    // 它起作用了

  case reflect.Array:
    // return a[:]
    // 它不起作用:无法对a进行切片(类型为interface{})
    //
    array, ok := a.([reflect.ValueOf(a).Len()]interface{})
    // :-((( 非常量数组边界reflect.ValueOf(a).Len()
    if ok {
       return array[:]
    }

  }
  return []interface{}(a)
}
英文:

I need to check if interface{} is an array and create corresponding slice if it is. Unfortunately I do not know array length in advance.

For example:

import (
  "reflect"
)
func AnythingToSlice(a interface{}) []interface{} {
  rt := reflect.TypeOf(a)
  switch rt.Kind() {
  case reflect.Slice:
    slice, ok := a.([]interface{})
    if ok {
      return slice
    }
    // it works
    
  case reflect.Array:
    // return a[:]
    // it doesn't work: cannot slice a (type interface {})   
    //
    array, ok := a.([reflect.ValueOf(a).Len()]interface{})
    // :-((( non-constant array bound reflect.ValueOf(a).Len()
    if ok {
       return array[:]
    }
    
  }
  return []interface{}(a)
}

答案1

得分: 3

在类型断言中需要显式指定类型。该类型不能通过反射构造。

除非参数是 []interface{} 类型,否则必须复制切片或数组以生成 []interface{}。

尝试这样做:

func AnythingToSlice(a interface{}) []interface{} {
    v := reflect.ValueOf(a)
    switch v.Kind() {
    case reflect.Slice, reflect.Array:
        result := make([]interface{}, v.Len())
        for i := 0; i < v.Len(); i++ {
            result[i] = v.Index(i).Interface()
        }
        return result
    default:
        panic("not supported")
    }
}

链接:https://play.golang.org/p/3bXxnHOK8_

英文:

An explicit type is required in a type assertion. The type cannot be constructed through reflection.

Unless the argument is a []interface{}, the slice or array must be copied to produce a []interface{}.

Try this:

func AnythingToSlice(a interface{}) []interface{} {
	v := reflect.ValueOf(a)
	switch v.Kind() {
	case reflect.Slice, reflect.Array:
		result := make([]interface{}, v.Len())
		for i := 0; i &lt; v.Len(); i++ {
			result[i] = v.Index(i).Interface()
		}
		return result
	default:
		panic(&quot;not supported&quot;)
	}
}

https://play.golang.org/p/3bXxnHOK8_

huangapple
  • 本文由 发表于 2017年8月15日 22:03:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/45694512.html
匿名

发表评论

匿名网友

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

确定