Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason why your program sometimes panics is that it is sometimes writing to <code>w http.ResponseWriter</code> after the program exits the <code>load</code> function. The <code>http</code> package automatically closes the <code>http.ResponseWriter</code> when the program exits the handler function. In function <code>writeContent</code>, the program will sometimes attempt to write to a closed <code>http.ResponseWriter</code>.</p> <p>BTW: You can make the program source code much smaller if you use the <code>io.Copy</code> function.</p> <p>To always get predictable behavior, make sure that <strong>all work</strong> that you want the program to perform in response to a HTTP request <strong>is done before you exit the handler function</strong>. For example:</p> <pre><code>func writeContent(w http.ResponseWriter, channel chan []byte) { fmt.Printf("ATTEMPTING TO WRITE CONTENT\n") for bytes := range channel { w.Write(bytes) fmt.Printf("BYTES RECEIVED\n") } fmt.Printf("FINISHED WRITING\n") } func load(w http.ResponseWriter, path string) { fmt.Printf("ATTEMPTING LOAD " + path + "\n") channel := make(chan []byte) workDone := make(chan byte) go func() { writeContent(w, channel) workDone &lt;- 1 //Send an arbitrary value }() go func() { getContent(path, channel) workDone &lt;- 2 //Send an arbitrary value }() &lt;-workDone &lt;-workDone } func handle(w http.ResponseWriter, r *http.Request) { fmt.Printf("HANDLING REQUEST FOR " + r.URL.Path[1:] + "\n") load(w, r.URL.Path[1:]) } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload