encoding/binary: Need more helpful error message when writing struct to a buffer rather than a short "invalid type XXX" #22860
Labels
FrozenDueToAge
help wanted
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
What version of Go are you using (
go version
)?go1.9.2
Does this issue reproduce with the latest release?
In my version of go, yes. And maybe yes for THE LATEST version: src/encoding/binary/binary.go:362
What operating system and processor architecture are you using (
go env
)?GOOS="darwin"
,GOARCH="amd64"
What did you do?
I was writing a program that writes a struct to a binary stream in order to communicate between C and Go using C's
struct
. See this playground as an example: https://play.golang.org/p/KG3WyuCiTjWhat did you expect to see?
Well, I wanted to see my encoded struct's byte stream, ...
What did you see instead?
... but what I saw was an un-intuitive error:
At the first time, I was like 'Ah, too bad. I cannot write struct directly into a byte buffer. There must be another way!' And what I found was this SO answer: https://stackoverflow.com/a/23176640/7804714
So the problem was that I should provide fixed-size(not just
int
;int16
, int32
, ...) members for struct. But the error message itself didn't provide any evidence of what is going wrong.In the documentation, it says:
But this still doesn't give any idea for struct's case(maybe it does, but me, couldn't catch that). I think
binary.Write: non fixed-size field of type *main.Person
would be much better. How do others think?The text was updated successfully, but these errors were encountered: