-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Description
When using (*xml.Encoder).EncodeToken, the Encoder
checks for mismatched elements. That is, it returns an error if e.g. a <a>
element is closed with a </b>
token or if there is an extra </c>
token after all elements have already been closed.
However, if the user simply forgets to close all elements, the Encoder
emits incorrect XML without reporting an error. See this code, for an example. So currently, the user must track the tokens they encode themselves, to make sure the emitted XML is valid.
Flush
returns an error
, but as it is possible to encode more tokens after Flush
has been called, it can't complain about unclosed elements itself.
I thus propose to add a new method to Encoder
:
// Close the Encoder, indicating that no more data will be written. It flushes any buffered XML to the underlying writer
// and returns an error if the written XML is invalid (e.g. by containing unclosed elements).
// After Close has been called, any Encode method return errors.
func (enc *Encoder) Close() error
Naming the method Close
is an indication that it should be called and the returned error should be checked by the user.