proposal: Go 2: text/template: return errors from HTMLEscape and JSEscape #29003
Labels
Go2Cleanup
Used by Ian and Robert for Go 2 organization. Unless you’re Ian or Robert, please do not use this.
Proposal
v2
A language change or incompatible library change
Milestone
template.HTMLEscape
andtemplate.JSEscape
each accept anio.Writer
and write to it. However, they ignore errors fromWrite
.That's fine when the destination
io.Writer
is one that cannot fail (such as a*bytes.Buffer
), but can mask real errors in general (see also #20803 (comment)).It is possible for the caller to detect those errors by wrapping the
io.Writer
, but wrapping anio.Writer
to capture an error that it already returns needlessly complicates the code.These functions should return errors, and leave the decision about whether those errors are safe to ignore up to the caller.
Compatibility
This change would be call-site compatible (leaving the vast majority of callers unchanged), but would break programs that pass or assign
template.HTMLEscape
ortemplate.JSEscape
as afunc(io.Writer, []byte)
. Such uses should be rare.As an alternative, we could add variants of those functions that do return errors; however, separate variants would mask missing error checks from analysis tools. I believe it would be better to simply change the signature in a Go 2 cleanup.
The text was updated successfully, but these errors were encountered: