英文:
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 < v.Len(); i++ {
			result[i] = v.Index(i).Interface()
		}
		return result
	default:
		panic("not supported")
	}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论