Last active
August 21, 2019 22:55
-
-
Save automaticalldramatic/9f256b8c5222a5cdfae15cd17aa58294 to your computer and use it in GitHub Desktop.
List out all the values in a struct
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
// testing output | |
reflectedStruct := reflect.ValueOf(event) | |
// in case you want to list all values stored at the location of this pointer to a struct | |
if reflectedStruct.Kind() == reflect.Ptr { | |
reflectedStruct = reflectedStruct.Elem() | |
} | |
structType := reflectedStruct.Type() | |
values := make(map[string]interface{}, reflectedStruct.NumField()) | |
for i := 0; i < reflectedStruct.NumField(); i++ { | |
values[structType.Field(i).Name] = reflectedStruct.Field(i).Interface() | |
log.Printf("%s: %v", structType.Field(i).Name, reflectedStruct.Field(i).Interface()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment