doc: add error handling on http.ListenAndServe

Fixes #19511

Change-Id: I5585726773b822dba0be0196961132323ebbe084
Reviewed-on: https://go-review.googlesource.com/53071
Reviewed-by: Chris Broadfoot <cbro@golang.org>
This commit is contained in:
Francesc Campoy Flores 2017-08-03 10:52:42 -07:00
parent 915126bb91
commit e3e09e3d5f
12 changed files with 27 additions and 11 deletions

View file

@ -8,6 +8,7 @@ import (
"errors" "errors"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"regexp" "regexp"
) )
@ -98,5 +99,5 @@ func main() {
http.HandleFunc("/view/", viewHandler) http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler) http.HandleFunc("/edit/", editHandler)
http.HandleFunc("/save/", saveHandler) http.HandleFunc("/save/", saveHandler)
http.ListenAndServe(":8080", nil) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -7,6 +7,7 @@ package main
import ( import (
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
) )
@ -49,5 +50,5 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
http.HandleFunc("/view/", viewHandler) http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler) http.HandleFunc("/edit/", editHandler)
http.ListenAndServe(":8080", nil) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -7,6 +7,7 @@ package main
import ( import (
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"regexp" "regexp"
) )
@ -87,5 +88,5 @@ func main() {
http.HandleFunc("/view/", makeHandler(viewHandler)) http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler)) http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler)) http.HandleFunc("/save/", makeHandler(saveHandler))
http.ListenAndServe(":8080", nil) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -7,6 +7,7 @@ package main
import ( import (
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
) )
@ -61,5 +62,5 @@ func main() {
http.HandleFunc("/view/", viewHandler) http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler) http.HandleFunc("/edit/", editHandler)
http.HandleFunc("/save/", saveHandler) http.HandleFunc("/save/", saveHandler)
http.ListenAndServe(":8080", nil) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -16,7 +16,7 @@
http.HandleFunc("/edit/", makeHandler(editHandler)) http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler)) http.HandleFunc("/save/", makeHandler(saveHandler))
! http.ListenAndServe(":8080", nil) ! log.Fatal(http.ListenAndServe(":8080", nil))
} }
--- 87,101 ---- --- 87,101 ----
http.HandleFunc("/edit/", makeHandler(editHandler)) http.HandleFunc("/edit/", makeHandler(editHandler))

View file

@ -7,6 +7,7 @@ package main
import ( import (
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"regexp" "regexp"
) )
@ -85,5 +86,5 @@ func main() {
http.HandleFunc("/edit/", makeHandler(editHandler)) http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler)) http.HandleFunc("/save/", makeHandler(saveHandler))
http.ListenAndServe(":8080", nil) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
) )
@ -11,5 +12,5 @@ func handler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -213,6 +213,12 @@ worry about its second parameter, <code>nil</code>, for now.)
This function will block until the program is terminated. This function will block until the program is terminated.
</p> </p>
<p>
<code>ListenAndServe</code> always returns an error, since it only returns when an
unexpected error occurs.
In order to log that error we wrap the function call with <code>log.Fatal</code>.
</p>
<p> <p>
The function <code>handler</code> is of the type <code>http.HandlerFunc</code>. The function <code>handler</code> is of the type <code>http.HandlerFunc</code>.
It takes an <code>http.ResponseWriter</code> and an <code>http.Request</code> as It takes an <code>http.ResponseWriter</code> and an <code>http.Request</code> as

View file

@ -7,6 +7,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
) )
@ -52,5 +53,5 @@ func editHandler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
http.HandleFunc("/view/", viewHandler) http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler) http.HandleFunc("/edit/", editHandler)
http.ListenAndServe(":8080", nil) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -7,6 +7,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
) )
@ -37,5 +38,5 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
http.HandleFunc("/view/", viewHandler) http.HandleFunc("/view/", viewHandler)
http.ListenAndServe(":8080", nil) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -7,6 +7,7 @@ package main
import ( import (
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
) )
@ -69,5 +70,5 @@ func main() {
http.HandleFunc("/view/", viewHandler) http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler) http.HandleFunc("/edit/", editHandler)
http.HandleFunc("/save/", saveHandler) http.HandleFunc("/save/", saveHandler)
http.ListenAndServe(":8080", nil) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -7,6 +7,7 @@ package main
import ( import (
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
) )
@ -53,5 +54,5 @@ func main() {
http.HandleFunc("/view/", viewHandler) http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler) http.HandleFunc("/edit/", editHandler)
//http.HandleFunc("/save/", saveHandler) //http.HandleFunc("/save/", saveHandler)
http.ListenAndServe(":8080", nil) log.Fatal(http.ListenAndServe(":8080", nil))
} }