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: add AppendByteOrder #50601

Closed
dsnet opened this issue Jan 13, 2022 · 8 comments
Closed

encoding/binary: add AppendByteOrder #50601

dsnet opened this issue Jan 13, 2022 · 8 comments

Comments

@dsnet
Copy link
Member

dsnet commented Jan 13, 2022

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

@dsnet dsnet added the Proposal label Jan 13, 2022
@gopherbot gopherbot added this to the Proposal milestone Jan 13, 2022
@seankhliao

This comment has been minimized.

@dsnet

This comment has been minimized.

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Jan 13, 2022
@rsc rsc moved this from Incoming to Active in Proposals (old) Jan 19, 2022
@rsc
Copy link
Contributor

rsc commented Jan 19, 2022

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Jan 26, 2022

Recently we've been doing things like ReadFileFS for FS with a ReadFile method.
How about type AppendByteOrder?
The -able seems out of place in Go.

Otherwise seems reasonable, and it's nice that LittleEndian and BigEndian have unexported types so we can add the methods and use them directly.

@rsc rsc changed the title proposal: encoding/binary: add AppendableByteOrder proposal: encoding/binary: add AppendByteOrder Feb 2, 2022
@rsc rsc moved this from Active to Likely Accept in Proposals (old) Feb 2, 2022
@rsc
Copy link
Contributor

rsc commented Feb 2, 2022

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

@rsc rsc moved this from Likely Accept to Accepted in Proposals (old) Feb 9, 2022
@rsc
Copy link
Contributor

rsc commented Feb 9, 2022

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— rsc for the proposal review group

@rsc rsc changed the title proposal: encoding/binary: add AppendByteOrder encoding/binary: add AppendByteOrder Feb 9, 2022
@rsc rsc modified the milestones: Proposal, Backlog Feb 9, 2022
@gopherbot
Copy link

Change https://go.dev/cl/386017 mentions this issue: binary: add AppendByteOrder

@gopherbot
Copy link

Change https://go.dev/cl/389636 mentions this issue: api: update next.txt

@dmitshur dmitshur modified the milestones: Backlog, Go1.19 Mar 3, 2022
gopherbot pushed a commit that referenced this issue Mar 3, 2022
CL 386017 added new API for encoding/binary package.
This file was accidentally not updated in the same CL.

Updates #50601

Change-Id: Iefeb596ba04b8c6576cf0fe42030f658a5848832
Reviewed-on: https://go-review.googlesource.com/c/go/+/389636
Trust: Joseph Tsai <joetsai@digital-static.net>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
@golang golang locked and limited conversation to collaborators Mar 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

5 participants