Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Forked from janhalfar/dump.go
Created April 17, 2019 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/beea545b6e2a7a7e5db1ab98099a1018 to your computer and use it in GitHub Desktop.
Save xeoncross/beea545b6e2a7a7e5db1ab98099a1018 to your computer and use it in GitHub Desktop.
golang reflection games
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