douqiao6015 2013-10-15 19:40
浏览 781
已采纳

Golang:获取切片的类型

I am using reflect package to get the type of arbitrary array, but getting

   prog.go:17: cannot use sample_array1 (type []int) as type []interface {} in function argument [process exited with non-zero status]

How do I get the type from array? I know how to get it from value.

  func GetTypeArray(arr []interface{}) reflect.Type {
      return reflect.TypeOf(arr[0])
  }

http://play.golang.org/p/sNw8aL0a5f

  • 写回答

2条回答 默认 最新

  • dsxmwin86342 2013-10-15 19:48
    关注

    Change:

    GetTypeArray(arr []interface{})
    

    to:

    GetTypeArray(arr interface{})
    

    By the way, []int is not an array but a slice of integers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?