Golang:获得片的类型 [英] Golang: get the type of slice

查看:160
本文介绍了Golang:获得片的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用reflect包获得任意阵列的类型,但得到

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

推荐答案

修改

GetTypeArray(arr []interface{})

GetTypeArray(arr interface{})

顺便说一句, [] INT 不是一个数组而是整数的

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

这篇关于Golang:获得片的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆