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
I'd like to see that the concept of request.Body is reconsidered. While on the server side(you have http.ResponseWriter and you only need to read the reqest.Body) it may be good to use It's annoying for the client side.
A request send from a client always requirs a reader to read from, but this is critical and expensive in multiple ways. Especially if you may upload larger data(ex: a large encoded json, a generated image) you kinda need to find a good encode logic(either you use 2 goroutines or some state machine which can encode up to N bytes per Write call) or you allocate a large buffer encode to it and send the data.
Example:
package main
import (
"bytes"
"fmt"
"io"
"net/http"
)
type SimpleBufWriter []byte
func (s *SimpleBufWriter) Write(w io.Writer) (int, error) {
return w.Write([]byte(*s))
}
func main() {
var (
buf = bytes.NewBuffer(nil)
data = SimpleBufWriter([]byte{0, 1})
)
//Tmp buffer is created
if _, err := data.Write(buf); err != nil {
fmt.Println("data.Write:", err)
return
}
/*
Body can be only a io.ReadCloser It would be so much easier to just pass a writer here
Or something like http.BodyWriter(w) with basicly returns errors for Read and Close but is forwarding Write just for the underlying writer
*/
req, err := http.NewRequest("POST", "http://example.ex/post", buf)
if err != nil {
fmt.Println("NewRequest:", err)
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Do:", err)
return
}
defer resp.Body.Close()
}
The text was updated successfully, but these errors were encountered:
@bradfitz
I got 1 full yr experience in go could you explain where It looks like I am still learning Go?
Your 'Hint' only works for a byte slice but a json would still require buffering. I can actually make a proof next week that there's a possible way to make this by far more efficent and only using a writer.
However I'll head over to the mailing list then I think.
Go Version: 1.5
OS: Linux x86
I'd like to see that the concept of request.Body is reconsidered. While on the server side(you have http.ResponseWriter and you only need to read the reqest.Body) it may be good to use It's annoying for the client side.
A request send from a client always requirs a reader to read from, but this is critical and expensive in multiple ways. Especially if you may upload larger data(ex: a large encoded json, a generated image) you kinda need to find a good encode logic(either you use 2 goroutines or some state machine which can encode up to N bytes per Write call) or you allocate a large buffer encode to it and send the data.
Example:
The text was updated successfully, but these errors were encountered: