On Tue, Jul 16, 2019 at 12:03 PM Hasan Genc <hasangenc.istan...@gmail.com>
wrote:

> This is my original code.
>
>
Multiple things are wrong with this code:

 * You are writing to a stream from multiple goroutines with no
synchronization.
 * You are not waiting for the goroutines to complete before returning.
Once the http handler returns, the response is written and the stream is
closed.

If you really want to use goroutines for this task, process the templates
in separate goroutines, collect the output via channels and write to the
output stream from the handler's goroutine.


> outside of the go routine w variable is
>
> [image: Screen Shot 2019-07-16 at 20.57.35.png]
>
>
>
>
>
>
>
>
>
>
> But inside the go routine
>
> [image: Screen Shot 2019-07-16 at 20.58.17.png]
>
>
>
>
>
>
>
>
> I don't understand why. Anyone can help about this ?
>
>
>
> func Make(w http.ResponseWriter, r *http.Request, app App) {
>    runtime.GOMAXPROCS(4)
>
>    flusher, ok := w.(http.Flusher)
>
>    if !ok {
>       panic("expected http.ResponseWriter to be an http.Flusher")
>    }
>
>    w.Header().Set("X-Content-Type-Options", "nosniff")
>
>    tmpl, err := template.New("app").Parse(app.Page.Content)
>
>    if err != nil {
>       panic("An Error occured when parsing html")
>       return
>    }
>
>    err = tmpl.Execute(w, "")
>
>    if err != nil {
>       panic("Error in Template.Execute")
>    }
>
>    flusher.Flush()
>
>    for _, gateway := range app.Gateway {
>
>       go func(gateway Gateway) {
>          tmpl, err := template.New(gateway.Name).Parse(gateway.Content)
>
>          if err != nil {
>             panic("An Error occured when parsing html")
>          }
>
>          err = tmpl.Execute(w, "")
>
>          if err != nil {
>             panic(err)
>          }
>
>          defer flusher.Flush()
>       }(gateway)
>    }
>
> }
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/17998013-2d1b-4921-8fa4-061a5b217875%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/17998013-2d1b-4921-8fa4-061a5b217875%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAMV2RqqhuiAQOKiJgtYHzajHv_KoJddnwJKZ1vZV8_esV_4%2B_g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to