binary.Read panic : reflect.Value.SetUint using value obtained using unexported field

1,853 views
Skip to first unread message

Jens-Uwe Mager

unread,
Dec 1, 2011, 2:08:05 PM12/1/11
to golan...@googlegroups.com
From looking at the docs of binary.Read/binary.Write I can not see that it will read or write only exported members. Interestingly, binary.Write happily writes all my fields, even though all are in lower case only. But on binary.Read, I get the above panic. Is this intentional? If yes, I will have to rename all my struct members I am attempting to Read/Write via binary.

Rob 'Commander' Pike

unread,
Dec 1, 2011, 2:10:30 PM12/1/11
to golan...@googlegroups.com

On Dec 1, 2011, at 11:08 AM, Jens-Uwe Mager wrote:

> From looking at the docs of binary.Read/binary.Write I can not see that it will read or write only exported members. Interestingly, binary.Write happily writes all my fields, even though all are in lower case only. But on binary.Read, I get the above panic. Is this intentional? If yes, I will have to rename all my struct members I am attempting to Read/Write via binary.

Reflection allows read access to unexported fields so, for instance, Printf can print the private elements of your data for debugging. But it does not allow write access, which (perhaps paradoxically) is what binary.Read needs.

-rob

Jens-Uwe Mager

unread,
Dec 1, 2011, 2:17:06 PM12/1/11
to golan...@googlegroups.com
Ok, thanks that explains it. Would be nice if binary would mention this, it is not obvious to the beginner in go.
Reply all
Reply to author
Forward
0 new messages