-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Description
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/KG3WyuCiTj
What 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:
panic: binary.Write: invalid type *main.Person
goroutine 1 [running]:
main.main()
/tmp/sandbox274417894/main.go:24 +0x180
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:
Data must be a fixed-size value or a slice of fixed-size values, or a pointer to such data.
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?