Created
September 11, 2015 06:27
-
-
Save kovetskiy/1e8b931b029f4fa5d633 to your computer and use it in GitHub Desktop.
golang handling panic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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