Skip to content

Instantly share code, notes, and snippets.

@sj14
Created September 26, 2023 09:00
Show Gist options
  • Save sj14/bbf68a51e99072c1c9c6719c6b922a46 to your computer and use it in GitHub Desktop.
Save sj14/bbf68a51e99072c1c9c6719c6b922a46 to your computer and use it in GitHub Desktop.
Print Go structure (e.g. all tags)
package main
func main() {
val := reflect.ValueOf(&MY_STRUCTURE).Elem()
printStructure(val, 4)
}
func printStructure(s reflect.Value, level int) {
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
for spaces := 0; spaces < level; spaces++ {
fmt.Print(" ")
}
fmt.Printf("%s\n", s.Type().Field(i).Tag)
if f.Kind() == reflect.Struct {
printStructure(f, level+4)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment