Skip to content

Commit

Permalink
net/http: document that [ListenAnd]Serve always returns a non-nil error
Browse files Browse the repository at this point in the history
Fixes #12229

Change-Id: I243e39f67748e6754fb7726b21b3afc1ff436771
Reviewed-on: https://go-review.googlesource.com/13780
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
adg committed Aug 20, 2015
1 parent 7c154d9 commit b733234
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
23 changes: 13 additions & 10 deletions src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1863,8 +1863,9 @@ func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {
}

// ListenAndServe listens on the TCP network address srv.Addr and then
// calls Serve to handle requests on incoming connections. If
// srv.Addr is blank, ":http" is used.
// calls Serve to handle requests on incoming connections.
// If srv.Addr is blank, ":http" is used.
// ListenAndServe always returns a non-nil error.
func (srv *Server) ListenAndServe() error {
addr := srv.Addr
if addr == "" {
Expand All @@ -1878,8 +1879,9 @@ func (srv *Server) ListenAndServe() error {
}

// Serve accepts incoming connections on the Listener l, creating a
// new service goroutine for each. The service goroutines read requests and
// new service goroutine for each. The service goroutines read requests and
// then call srv.Handler to reply to them.
// Serve always returns a non-nil error.
func (srv *Server) Serve(l net.Listener) error {
defer l.Close()
var tempDelay time.Duration // how long to sleep on accept failure
Expand Down Expand Up @@ -1957,11 +1959,10 @@ func (s *Server) logf(format string, args ...interface{}) {
//
// func main() {
// http.HandleFunc("/hello", HelloServer)
// err := http.ListenAndServe(":12345", nil)
// if err != nil {
// log.Fatal("ListenAndServe: ", err)
// }
// log.Fatal(http.ListenAndServe(":12345", nil))
// }
//
// ListenAndServe always returns a non-nil error.
func ListenAndServe(addr string, handler Handler) error {
server := &Server{Addr: addr, Handler: handler}
return server.ListenAndServe()
Expand Down Expand Up @@ -1989,12 +1990,12 @@ func ListenAndServe(addr string, handler Handler) error {
// http.HandleFunc("/", handler)
// log.Printf("About to listen on 10443. Go to https://127.0.0.1:10443/")
// err := http.ListenAndServeTLS(":10443", "cert.pem", "key.pem", nil)
// if err != nil {
// log.Fatal(err)
// }
// log.Fatal(err)
// }
//
// One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem.
//
// ListenAndServeTLS always returns a non-nil error.
func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Handler) error {
server := &Server{Addr: addr, Handler: handler}
return server.ListenAndServeTLS(certFile, keyFile)
Expand All @@ -2010,6 +2011,8 @@ func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Han
// certificate, any intermediates, and the CA's certificate.
//
// If srv.Addr is blank, ":https" is used.
//
// ListenAndServeTLS always returns a non-nil error.
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
addr := srv.Addr
if addr == "" {
Expand Down
5 changes: 1 addition & 4 deletions src/net/http/triv.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,5 @@ func main() {
http.HandleFunc("/args", ArgServer)
http.HandleFunc("/go/hello", HelloServer)
http.HandleFunc("/date", DateServer)
err := http.ListenAndServe(":12345", nil)
if err != nil {
log.Panicln("ListenAndServe:", err)
}
log.Fatal(http.ListenAndServe(":12345", nil))
}

0 comments on commit b733234

Please sign in to comment.