go - Get name of struct field using reflection -



go - Get name of struct field using reflection -

what way of printing "foo" here? in example, prints "string".

http://play.golang.org/p/znk6prwepp

type struct { foo string } func (a *a) printfoo(){ fmt.println("foo value " + a.foo) } func main() { := &a{foo: "afoo"} val := reflect.indirect(reflect.valueof(a)) fmt.println(val.field(0).type().name()) }

you want val.type().field(0).name. field method on reflect.type homecoming struct describing field, includes name, among other information.

there no way retrieve field name reflect.value representing particular field value, since property of containing struct.

go

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -