Created
September 26, 2023 09:00
-
-
Save sj14/bbf68a51e99072c1c9c6719c6b922a46 to your computer and use it in GitHub Desktop.
Print Go structure (e.g. all tags)
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 | |
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