Last active
April 26, 2018 13:15
-
-
Save vmpartner/dcc271f70f452b79760c4b647c588091 to your computer and use it in GitHub Desktop.
golang iterate struct keys
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
v := reflect.ValueOf(client) | |
for i := 0; i < v.NumField(); i++ { | |
key := v.Type().Field(i).Name | |
value := "" | |
switch v.Field(i).Type().String() { | |
case "int64": | |
value = strconv.Itoa(int(v.Field(i).Int())) | |
case "sql.NullString": | |
value = v.Field(i).FieldByName("String").String() | |
case "sql.NullInt64": | |
value = v.Field(i).FieldByName("Int64").String() | |
} | |
fmt.Printf("%+v: %+v\n", key, value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment