Created
March 21, 2019 07:50
-
-
Save kitz99/e70a560ff1366979d93da6726115031b to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"reflect" | |
) | |
type User struct { | |
ID int | |
Email string `json:"email" yaml:"email_address"` | |
Data struct { | |
Key string | |
Value bool | |
} | |
} | |
func main() { | |
u := User{ID: 42} | |
addrU := &u | |
uType := reflect.TypeOf(u) | |
addrType := reflect.TypeOf(addrU) | |
fmt.Printf("Type of u: %s | kind: %s\n", uType.Name(), uType.Kind()) | |
fmt.Printf("Type of addrU: %s | kind: %s\n", addrType.Elem(), addrType.Kind()) | |
fmt.Println("\n<Inspect struct u>\n") | |
for i := 0; i < uType.NumField(); i++ { | |
field := uType.Field(i) | |
fmt.Printf("Type of field %s: %s | kind: %s\n", field.Name, field.Type.Name(), field.Type.Kind()) | |
if field.Tag != "" { | |
fmt.Printf("\tTags for %s:\n", field.Name) | |
fmt.Printf("\t\t->json: %s\n\t\t->yaml: %s\n", field.Tag.Get("json"), field.Tag.Get("yaml")) | |
} | |
if field.Type.Kind() == reflect.Struct { | |
for j := 0; j < field.Type.NumField(); j++ { | |
subField := field.Type.Field(j) | |
fmt.Printf("\tType of subfield %s: %s | kind: %s\n", subField.Name, subField.Type.Name(), subField.Type.Kind()) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment