-
Notifications
You must be signed in to change notification settings - Fork 36
Open
Description
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
- 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.
- 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.
- Brittle API: It requires the caller to know the exact trailing bytes of a dynamic template or HTML fragment to ensure the buffer clears.
- 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
- Mark as deprecated to save others from wasting time on this
- Document the limitations
- Document correct usage
Current docs do not set the last element: https://github.com/yosssi/gohtml?tab=readme-ov-file#format-go-htmltemplate-packages-templates-execute-result
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels