-
-
Save rgaquino/33f30d5c840caa0a01f076c61d61242c to your computer and use it in GitHub Desktop.
Revisions
-
rgaquino revised this gist
Nov 7, 2019 . 1 changed file with 15 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,21 +5,29 @@ import ( "reflect" ) type Bar struct { Gender string `tag_name:"gender"` } type Foo struct { FirstName string `tag_name:"tag 1"` LastName string `tag_name:"tag 2"` Age int `tag_name:"tag 3"` Others *Bar `tag_name:"tag 4"` } func doReflection(f interface{}) { val := reflect.ValueOf(f).Elem() for i := 0; i < val.NumField(); i++ { valueField := val.Field(i) typeField := val.Type().Field(i) tag := typeField.Tag if valueField.Kind() == reflect.Ptr { fmt.Println("Found a pointer!") doReflection(valueField.Interface()) } fmt.Printf("Field Name: %s,\t Field Type: %v,\t Field Value: %v,\t Tag Value: %s\n", typeField.Name, typeField.Type, valueField.Interface(), tag.Get("tag_name")) } } @@ -28,7 +36,10 @@ func main() { FirstName: "Drew", LastName: "Olson", Age: 30, Others: &Bar{ Gender: "male", }, } doReflection(f) } -
drewolson revised this gist
Feb 13, 2013 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,11 +15,11 @@ func (f *Foo) reflect() { val := reflect.ValueOf(f).Elem() for i := 0; i < val.NumField(); i++ { valueField := val.Field(i) typeField := val.Type().Field(i) tag := typeField.Tag fmt.Printf("Field Name: %s,\t Field Value: %v,\t Tag Value: %s\n", typeField.Name, valueField.Interface(), tag.Get("tag_name")) } } -
drewolson revised this gist
Feb 13, 2013 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ package main import ( "fmt" "reflect" ) @@ -26,9 +26,9 @@ func (f *Foo) reflect() { func main() { f := &Foo{ FirstName: "Drew", LastName: "Olson", Age: 30, } f.reflect() } -
drewolson created this gist
Feb 12, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ package main import ( "fmt" "reflect" ) type Foo struct { FirstName string `tag_name:"tag 1"` LastName string `tag_name:"tag 2"` Age int `tag_name:"tag 3"` } func (f *Foo) reflect() { val := reflect.ValueOf(f).Elem() for i := 0; i < val.NumField(); i++ { value_field := val.Field(i) type_field := val.Type().Field(i) tag := type_field.Tag fmt.Printf("Field Name: %s,\t Field Value: %v,\t Tag Value: %s\n", type_field.Name, value_field.Interface(), tag.Get("tag_name")) } } func main() { f := &Foo{ FirstName: "Drew", LastName: "Olson", Age: 30, } f.reflect() }