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

proposal: encoding/binary: Read (or a new call) should return number of bytes read #18585

Open
sayotte opened this issue Jan 10, 2017 · 5 comments
Labels
Go2Cleanup Used by Ian and Robert for Go 2 organization. Unless you’re Ian or Robert, please do not use this. Proposal v2 A language change or incompatible library change
Milestone

Comments

@sayotte
Copy link

sayotte commented Jan 10, 2017

Please answer these questions before submitting your issue. Thanks!

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

1.7.4

What operating system and processor architecture are you using (go env)?

linux/amd64
linux/arm
darwin/amd64

What did you do?

I want to use binary.Read() to marshall data to/from network byte-order.
I must keep track of my offset from the beginning of the stream (I'm decoding DNS records, which sometimes refer to strings found at offsets-from-0 farther back as a form of compression).
I would prefer not to require the use of an io.ReadSeeker just for the sake of discovering my current offset.

What did you expect to see?

I expected binary.Read() to return the number of bytes read, as all of the io.Read implementations do.

What did you see instead?

It doesn't! This seems like a silly oversight, but also not a big or difficult change.

I created a trivial library to replace the function. In retrospect, it would be better to have a differently-named function in the upstream Golang binary package so that Go-1.x compatibility isn't broken. binary.Read could be a wrapper which just drops the bytesRead return value.

@bradfitz
Copy link
Contributor

Whether or not the API is a mistake, it's frozen now, as you've noted.

The other possibility is you can pass in an io.Reader wrapper that counts how many bytes have been read through it.

It doesn't seem worth adding more API to the package at this point.

@sayotte
Copy link
Author

sayotte commented Jan 10, 2017

@bradfitz somehow that possibility hadn't occurred to me, guess I'm still getting used to satisfying interfaces.

The workaround will definitely suffice but I still hate the inconsistency. Is there a Wish List of things the Go team would like to get more right in a Go-2.0, if such a thing ever comes to be?

Thanks for the swift response!

@ianlancetaylor
Copy link
Contributor

The wishlist is issues with the "go2" label.

@sayotte
Copy link
Author

sayotte commented Jan 10, 2017

@ianlancetaylor can we add that label here, rather than closing it as a "let's never fix that"?

@ianlancetaylor ianlancetaylor changed the title binary.Read (or a new call) should return number of bytes read encoding/binary: Read (or a new call) should return number of bytes read Jan 10, 2017
@ianlancetaylor ianlancetaylor added the v2 A language change or incompatible library change label Jan 10, 2017
@ianlancetaylor
Copy link
Contributor

@sayotte Done.

@dsnet dsnet added this to the Unplanned milestone Jan 10, 2017
@rsc rsc changed the title encoding/binary: Read (or a new call) should return number of bytes read proposal: encoding/binary: Read (or a new call) should return number of bytes read Jun 17, 2017
@ianlancetaylor ianlancetaylor added the Go2Cleanup Used by Ian and Robert for Go 2 organization. Unless you’re Ian or Robert, please do not use this. label Jan 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Go2Cleanup Used by Ian and Robert for Go 2 organization. Unless you’re Ian or Robert, please do not use this. Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

5 participants