Skip to content

NewWriter causes silent data loss and provides no streaming benefit #25

@nirs

Description

@nirs

NewWriter is an unreliable wrapper that buffers the entire input in memory and only flushes when a specific sentinel string is matched. It should be deprecated in favor of Format.

The Problems

  1. Silent Data Loss: The Writer only flushes to the destination if the input ends with the exact string set in SetLastElement. Because it lacks a Close() or Flush() method, data is frequently left trapped in the internal buffer.
  2. False Streaming: It does not process data in chunks. It buffers the entire payload into an internal bytes.Buffer before formatting. It has the same memory overhead as Format(string) but with added complexity.
  3. Brittle API: It requires the caller to know the exact trailing bytes of a dynamic template or HTML fragment to ensure the buffer clears.
  4. Unexpected API: No user expect that a writer has a SetLastElement. Using io.WriteCloser is the expected interface

Reproducer

var buf bytes.Buffer
w := gohtml.NewWriter(&buf)
fmt.Fprint(w, "<html><body>Hello</body></html>")
// buf.String() is "" because SetLastElement was not configured.

Suggested fix

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions