title: http: multiple response.WriteHeader calls
body: <p>I am currently receiving this <code>http: multiple response.WriteHeader calls</code> error while trying to send back a response to Angular. The main thing I'm doing is sending a post request from Angular to Go. Go then inserts the data received into mongoDB, but if the username already exists I change <code>dup="true"</code> and try to send a custom response.  </p>
  
  <pre><code>func Register(w http.ResponseWriter, req *http.Request) {
  
  u := req.FormValue("username")
  p := req.FormValue("password")
  e := req.FormValue("email")
  n := req.FormValue("name")
  
  err := tpl.ExecuteTemplate(w, "index.html", User{u, p, e, n})
  if err != nil {
      http.Error(w, err.Error(), 500)
      log.Fatalln(err)
  }
  
  a := User{Username: u, Password: p, Email: e, Name: n}
  if a.Username != "" || a.Password != "" || a.Email != "" || a.Name != "" {
      insert(a)
      if dup == "true" {
          w.WriteHeader(http.StatusInternalServerError)
      }
  }}
  </code></pre>
  
  <p><code>w.WriteHeader(http.StatusInternalServerError)</code> is just an example; if I use anything with write header I get the same <code>http: multiple response.WriteHeader calls</code></p>