All possible kinds: http://golang.org/pkg/reflect/#Kind
If you know something simpler, let me know...
Code:
package main
import ("fmt"
"reflect")
func isArray(a interface{}) bool {
var v reflect.Value
v = reflect.ValueOf(a)
fmt.Println("a value: %T %v", v, v)
var k reflect.Kind
k = v.Kind()
fmt.Println("a kind: %T %v", k, k)
if (k == reflect.Array) {
return true
}
return false
}
func main() {
a := [...]string{"1","2"}
//a := "fred"
if (isArray(a)) {
fmt.Println("a is an array")
} else {
fmt.Println("a is NOT an array")
}
t := reflect.TypeOf(a)
fmt.Println("a type: %T %v", t, t)
var v reflect.Value
v = reflect.ValueOf(a)
fmt.Println("a value: %T %v", v, v)
var k reflect.Kind
k = v.Kind()
fmt.Println("a kind: %T %v", k, k)
}
Run:
a value: %T %v <[2]string Value> <[2]string Value>
a kind: %T %v array array
a is an array
a type: %T %v [2]string [2]string
a value: %T %v <[2]string Value> <[2]string Value>
a kind: %T %v array array
No comments:
Post a Comment