Skip to content

Instantly share code, notes, and snippets.

@kovetskiy
Created September 11, 2015 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kovetskiy/1e8b931b029f4fa5d633 to your computer and use it in GitHub Desktop.
Save kovetskiy/1e8b931b029f4fa5d633 to your computer and use it in GitHub Desktop.
golang handling panic
package main
import (
"log"
"net/http"
"sync"
)
type Handler struct{}
func (handler Handler) ServeHTTP(
response http.ResponseWriter, request *http.Request,
) {
defer func() {
if err := recover(); err != nil {
log.Printf("do not panic, please")
}
}()
workers := &sync.WaitGroup{}
// there is 1, not 2
workers.Add(1)
go func() {
workers.Done()
}()
go func() {
workers.Done()
//there is will be error 'negative WaitGroup counter'
}()
workers.Wait()
}
func main() {
handler := Handler{}
http.ListenAndServe(":9090", handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment