Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/binary: Need more helpful error message when writing struct to a buffer rather than a short "invalid type XXX" #22860

hallazzang opened this issue Nov 23, 2017 · 3 comments
help wanted NeedsFix The path to resolution is known, but the work has not been done.


Copy link

hallazzang commented Nov 23, 2017

What version of Go are you using (go version)?


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:

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]:
	/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:

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?

@ianlancetaylor ianlancetaylor added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Mar 29, 2018
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Mar 29, 2018
Copy link

gopherbot commented May 14, 2018

Change mentions this issue: encoding/binary : fix error message for Write

Copy link

iwdgo commented May 14, 2018

Submitted fix provides a message referring to documented constraint

Copy link

rafaelvanoni commented Jul 24, 2021

I agree that this would be a good enhancement. If any maintainer agrees, I'd be happy to propose a patch. Please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
help wanted NeedsFix The path to resolution is known, but the work has not been done.
None yet

No branches or pull requests

5 participants