golang/go

doc: Writing web applications ignores error from ListenAndServe

joneskoo opened this issue · 4 comments

@campoy:
Does everyone forget to check the error on ListenAndServe? #golang
https://twitter.com/francesc/status/838547440710012928

Nothing in this tutorial https://golang.org/doc/articles/wiki/ Writing Web Applications, in the Articles section of documentation, seems to indicate that ListenAndServe may return an error. Errors should be checked.

https://golang.org/pkg/net/http/#ListenAndServe
func ListenAndServe(addr string, handler Handler) error
ListenAndServe always returns a non-nil error.

Since I don't see an issue opened by @campoy , I'm opening one.

EDIT: Clarified that the issue is in Article, not the package documentation.

qqiao commented

The error isn't ignored in the example. It was first logged, then caused an os.Exit(1)

https://golang.org/src/log/log.go?s=9066:9094#L291

The documentation pointed by @joneskoo is indeed failing silently if http.ListenAndServe returns an error.
Since we're not using the Shutdown method, I'd say that it's safe to simply add log.Fatal.
I'll send a CL

Change https://golang.org/cl/53071 mentions this issue: docs: add error handling on http.ListenAndServe