You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This proposal suggests the addition of the CopyReader function to the io package in the Go standard library.
Background
When processing HTTP requests, both middleware and handlers often need to read the request body. However, as the io.Reader interface, from which the body is read, is a forward-only stream, you run into an issue if you need to read it more than once. Once the body is read in the handler (endpoint), it has been consumed and cannot be read again. This characteristic of io.Reader causes difficulty because there's no capability for re-reading a stream that's already been read.
Proposal
CopyReader is a function in io package.
CopyReader is got 2 parameters src / dst that is io.Reader type.
CopyReader return error
CopyReader example is below.
vardst io.Readersrc:=strings.NewReader("hogehoge")
err:=io.CopyReader(src, dst) // dst has hogehoge
Rationale
Adding an io.CopyReader function to the io package would address a common pain point in working with io.Reader interfaces: the inability to re-read an already consumed stream. This is particularly troublesome when developing middleware for HTTP servers where both the middleware and the handler want to read the request body. Once the body is consumed by the io.Reader in the handler, it cannot be read again in the middleware, which limits the ability to process request data in middleware functionality such as logging or data validation.
It encourages developers to read from the io.Reader freely, without having to excessively plan ahead or manage temporary workaround solutions that is implemented by bytes/io.NopCloser. This gives more freedom to write more readable and maintainable code.
The text was updated successfully, but these errors were encountered:
proposal: io: CopyReader to the io package
Abstract
This proposal suggests the addition of the CopyReader function to the io package in the Go standard library.
Background
When processing HTTP requests, both middleware and handlers often need to read the request body. However, as the io.Reader interface, from which the body is read, is a forward-only stream, you run into an issue if you need to read it more than once. Once the body is read in the handler (endpoint), it has been consumed and cannot be read again. This characteristic of io.Reader causes difficulty because there's no capability for re-reading a stream that's already been read.
Proposal
CopyReader example is below.
Rationale
Adding an io.CopyReader function to the io package would address a common pain point in working with io.Reader interfaces: the inability to re-read an already consumed stream. This is particularly troublesome when developing middleware for HTTP servers where both the middleware and the handler want to read the request body. Once the body is consumed by the io.Reader in the handler, it cannot be read again in the middleware, which limits the ability to process request data in middleware functionality such as logging or data validation.
It encourages developers to read from the io.Reader freely, without having to excessively plan ahead or manage temporary workaround solutions that is implemented by bytes/io.NopCloser. This gives more freedom to write more readable and maintainable code.
The text was updated successfully, but these errors were encountered: