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: feature request: support strings in the same way as []byte #12362

Closed
alandonovan opened this issue Aug 27, 2015 · 4 comments
Closed

Comments

@alandonovan
Copy link
Contributor

It would be useful to be able to encode struct { FileName string; Content []byte } values, but this is currently impossible with encoding/binary.

The package can encode []byte values but not strings. This seems rather arbitrary. Supporting strings, with the same encoding as []byte(s), would save making an often-large copy in many cases.

The encoding/binary package can also encode structs whose fields are scalar numbers, but not structs whose fields are []byte slices. Is there any particular reason for this? It doesn't seem like it would be hard to change.

@minux
Copy link
Member

minux commented Aug 27, 2015 via email

@alandonovan
Copy link
Contributor Author

No variable-length fields are currently supported. I hadn't realized at first that Writing a []byte doesn't write the length; the user must do this. Now the limitations make more sense.

That said, preceding []bytes and strings with a varint-encoded length seems kinda obvious.

@alandonovan alandonovan changed the title encoding/binary: feature request: support structs containing []byte slices and strings encoding/binary: feature request: support strings in the same way as []byte Aug 27, 2015
@alandonovan
Copy link
Contributor Author

I retract the request to support []byte and strings as struct fields, since it's at the wrong level of abstraction. encoding/gob makes more sense for that.

It might still make sense for encoding/binary to support strings in the same way as []byte, though.

@rsc
Copy link
Contributor

rsc commented Oct 23, 2015

That said, preceding []bytes and strings with a varint-encoded length seems kinda obvious.

Which varint encoding? Or fixed32? Or fixed64? And so on.

encoding/binary handles []uint8 (akak []byte) because it handles the general case of slice of fixed-size-integer. That is, the same code handles []uint16, []uint32, and []uint64. While we could add string, it would be a special case and one that I think would confuse people more than it would help.

Because of the general case, it would be silly to remove support for []uint8 as a special case. But at the same time I don't think it makes sense to add a special case for string. Instead of binary.Write(w, binary.LittleEndian, s), use w.Write([]byte(s)) or io.WriteString. That's much clearer and doesn't make one wonder how exactly it's important that the string is written in little-endian format.

@rsc rsc closed this as completed Oct 23, 2015
@golang golang locked and limited conversation to collaborators Oct 24, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants