Skip to content

Instantly share code, notes, and snippets.

@ryochack
Created March 16, 2014 13:28
Show Gist options
  • Save ryochack/9583234 to your computer and use it in GitHub Desktop.
Save ryochack/9583234 to your computer and use it in GitHub Desktop.
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);
}
@ryochack
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment