reflect.Type.Field() order

692 views
Skip to first unread message

aka...@keyba.se

unread,
Jun 13, 2017, 12:34:03 PM6/13/17
to golang-nuts
https://golang.org/pkg/reflect/#Value.Field says "Field returns the i'th field of the struct v.". Are there any guarantees as to what the ordering of the i'th field is? If not, does anyone know what the current behavior is, and whether it may change in the future? Is there a way to get the fields in declaration order?

Looking around online turned up https://stackoverflow.com/questions/32392311/reflect-type-field-order , which didn't come to any conclusion.

I'm asking because a serialization library I'm using uses the above field order when serializing structs as arrays, and I encountered a case where it seems to diverge from declaration order. If this is something that may change in the future, the users of that library (and anything like it) would probably break...

-- Fred

Ian Lance Taylor

unread,
Jun 13, 2017, 3:33:00 PM6/13/17
to aka...@keyba.se, golang-nuts
It's the order in which the fields appear in the struct declaration.
It's not going to change. If you find a case where it is *not* the
order in the declaration, please file a bug. Thanks.

Ian

aka...@keyba.se

unread,
Jun 13, 2017, 4:58:30 PM6/13/17
to golang-nuts, aka...@keyba.se
On Tuesday, June 13, 2017 at 2:33:00 PM UTC-5, Ian Lance Taylor wrote:

It's the order in which the fields appear in the struct declaration.
It's not going to change.  If you find a case where it is *not* the
order in the declaration, please file a bug.  Thanks.

Ian

Thanks for the clarification. After some further digging, this turned out to be a bug in how the serialization library handles embedded structs.

-- Fred
Reply all
Reply to author
Forward
0 new messages