ryochack
3/16/2014 - 1:28 PM

golang reflection: interface of array

golang reflection: interface of array

package main

import (
	"fmt"
	"reflect"
)

func dump_interface_array(args interface{}) {
	val := reflect.ValueOf(args)
	fmt.Println(val.Kind())
	if val.Kind() == reflect.Array {
		fmt.Println("len = ", val.Len())
		for i:=0; i<val.Len(); i++ {
			e := val.Index(i)
			switch e.Kind() {
			case reflect.Int:
				fmt.Printf("%v, ", e.Int())
			case reflect.Float32:
				fallthrough
			case reflect.Float64:
				fmt.Printf("%v, ", e.Float())
			default:
				panic(fmt.Sprintf("invalid Kind: %v", e.Kind()))
			}
		}
		fmt.Println()
	}
}

func main() {
	int_ary := [4]int{1, 2, 3, 4}
	float32_ary := [4]float32{1.1, 2.2, 3.3, 4.4}
	float64_ary := [4]float64{1.1, 2.2, 3.3, 4.4}

	dump_interface_array(int_ary);
	dump_interface_array(float32_ary);
	dump_interface_array(float64_ary);
}