-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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/base64: add AppendEncode and AppendDecode methods to *Encoding #19366
Comments
Prepared the CL |
CL https://golang.org/cl/37639 mentions this issue. |
The appending functions in strconv allow callers to avoid the string allocation. That's not necessary with encoding/base64: Encode and Decode already operate on caller-provided buffers. This change wouldn't allow you to do something you can't do today. It's a minor convenience issue which I don't think warrants additional API surface. |
The current API requires at least three additional steps from the caller in order to use // 1. Determine the required dst buffer size:
dstLen := enc.EncodedLen(len(src))
// 2. Allocate the dst buffer
dst := make([]byte, dstLen)
// 3. Encode src into dst
enc.Encode(dst, src)
// 4. Use dst. Usually this means appending dst to another buffer
someBuf = append(someBuf, dst...) Compare it to the equivalent one-liner if this proposal will be accepted: someBuf = enc.AppendEncode(someBuf, src) Additional benefits:
|
Below are random links to real code found on github via this search, which could benefit from https://github.com/twstrike/otr3/blob/88562eb4677900a21ecfa13a8c92613dd79b505d/b64.go IMHO, |
Upgraded this to a proposal. @golang/proposal-review, there is also some discussion in https://golang.org/cl/37639 but let's keep it here. |
I think what you really want is:
Or something like that. It's unclear this needs to be in the standard library. |
Based on not much interest and on the fact that one can already write this function without additional help from the standard library, I'm leaning toward declining this proposal. Objections? |
No objections. |
The problem
Currently
encoding/base64.Encoding
provides not very convenient Encode and Decode methods when working withdst
byte slices. These methods require the caller to pre-allocatedst
byte slice with the space required for encoding or decoding to succeed. This significantly complicates the resulting code, which should just append base64-encoded (or -decoded) value to the provideddst
byte slice. For instance:The solution
It would be great if
encoding/base64
would provideAppendEncode
andAppendDecode
methods on theEncoding
with the following signatures:This would simplify and optimize third-party code that currently uses
Encode
andDecode
methods ofEncoding
struct.Standard go packages already contains similar
Append*
functions - seestrconv.Append*
for details.The text was updated successfully, but these errors were encountered: