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

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

Comments

@hallazzang
Copy link
Contributor

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?

@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
@gopherbot
Copy link

Change https://golang.org/cl/113075 mentions this issue: encoding/binary : fix error message for Write

@iwdgo
Copy link
Contributor

iwdgo commented May 14, 2018

Submitted fix provides a message referring to documented constraint

@rafaelvanoni
Copy link

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.

@golang golang locked and limited conversation to collaborators Apr 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants