-
-
Save automaticalldramatic/c6c3d45b90825c5379a61f08a09c6adf to your computer and use it in GitHub Desktop.
golang reflection: interface of array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment