-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Description
A common operation when dealing with binary wire formats is the need to append a fixed-width integer.
Currently, a common way to do it is with:
var arr [4]byte
binary.LittleEndian.PutUint32(arr[:], val)
out = append(out, arr[:]...)
This takes 3 lines and cannot be done as a expression.
I propose the addition of a new interface:
type AppendableByteOrder interface {
AppendUint16([]byte, uint16) []byte
AppendUint32([]byte, uint32) []byte
AppendUint64([]byte, uint64) []byte
String() string
}
And that we document that LittleEndian
and BigEndian
both implement ByteOrder
and AppendableByteOrder
.
Thus, the lines above can be simplified as:
out = binary.LittleEndian.AppendUint32(out, val)
In my work with various binary wire formats (e.g., tar, protobuf, etc.), an API like this would be more convenient than the existing PutUintX
APIs.
The AppendableByteOrder
interface exists primarily for documentation purposes. It is not strictly needed.
\cc @josharian
mvdan, zephyrtronium, ericchiang, mpx, qmuntal and 11 more4ad