Skip to content
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: io: add LimitWriter #54548

Closed
earthboundkid opened this issue Aug 19, 2022 · 1 comment
Closed

proposal: io: add LimitWriter #54548

earthboundkid opened this issue Aug 19, 2022 · 1 comment

Comments

@earthboundkid
Copy link
Contributor

In #51115, a proposal to add an error field to io.LimitedReader was accepted, but then the change was reverted when it turned out to have some tricky corner cases around the last byte.

As a new proposal, which may or may not supersede the change to io.LimitedReader, I suggest adding an io.LimitWriter function with approximately this implementation:

func LimitWriter(w io.Writer, limit int64, err error) io.Writer {
	return &limitedWriter{w, limit, err}
}

type limitedWriter struct {
	w io.Writer
	n int64
	e error
}

func (lw *limitedWriter) Write(p []byte) (int, error) {
	if lw.n < 1 {
		return 0, lw.e
	}
	if lw.n < int64(len(p)) {
		p = p[:lw.n]
	}
	n, err := lw.w.Write(p)
	lw.n -= int64(n)
	return n, err
}
@gopherbot gopherbot added this to the Proposal milestone Aug 19, 2022
@seankhliao
Copy link
Member

Duplicate of #54111

@seankhliao seankhliao marked this as a duplicate of #54111 Aug 19, 2022
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 19, 2022
@golang golang locked and limited conversation to collaborators Aug 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants