Created
December 10, 2015 09:44
-
-
Save janhalfar/7585565ab31f163fc071 to your computer and use it in GitHub Desktop.
golang reflection games
This file contains 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
func dump(v reflect.Value, path string) { | |
switch v.Type().Kind() { | |
case reflect.Interface: | |
if v.Elem().Type().Kind() == reflect.Interface { | |
fmt.Println(path, ":", "merry xmas an interface") | |
} else { | |
dump(v.Elem(), path) | |
} | |
case reflect.Int, reflect.Int64, reflect.Float64: | |
fmt.Println(path, ":", "it is a number", v.String()) | |
case reflect.String: //reflect.Int, | |
fmt.Println(path, ":", "it is string:", v.String()) | |
case reflect.Bool: //reflect.Int, | |
fmt.Println(path, ":", "it is bool:", v.Bool()) | |
case reflect.Struct: | |
fmt.Println(path, ":", "it is a struct") | |
case reflect.Map: | |
fmt.Println(path, ":", "it is a map") | |
for _, key := range v.MapKeys() { | |
dump(v.MapIndex(key), path+"/"+key.String()) | |
} | |
case reflect.Slice: | |
fmt.Println(path, ":", "it is a slice") | |
default: | |
fmt.Println(path, ":", "unknown type", v.Type()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment