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
Comments
Does Read/Write support byte slices and strings in structs now? I don't
think they support any non-fixed size fields.
Supporting non-fixed size fileds means we need to marshal the length
explicitly. Is there a consensus on the format? Varint seems reasonable,
but others might disagree.
|
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. |
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. |
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. |
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.
The text was updated successfully, but these errors were encountered: