-
-
Save drewolson/4771479 to your computer and use it in GitHub Desktop.
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++ { | |
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")) | |
} | |
} | |
func main() { | |
f := &Foo{ | |
FirstName: "Drew", | |
LastName: "Olson", | |
Age: 30, | |
} | |
f.reflect() | |
} |
Thank you very much!
Just helped me.
For ref - http://play.golang.org/p/P9zvVJnMhR
Thanks very much for this! Very helpful.
Nice snippet, thanks!
Thank you very much! Just needed it.
Does the reflection logic change if foo is passed in as an empty interface? interface{}?
Thanks!
Thank you! 👍
很好的例子,感谢作者!
thanks to the author,A good example!
thanks @drewolson, and @lenw for posting the snippet
yeah this helped me a lot!!
This is a good example. Thanks.
good example. tks
This is a good example. It helped me!
So nice example. Hah, solve my problem perfectly
Line 15: Can panic.
For zero Value.
See https://golang.org/pkg/reflect/#ValueOf
https://play.golang.org/p/AcFBIdQeAhi
This is a very good example of reflection. Thank you
Thanks!!!
It is what exactly I wanted. :) It helps to save my time a lot. Thanks.
nice example!!!
Thank you! It's helpful for me!
Thank You :)
Great, thanks!
Thanks!