doc: add error handling on http.ListenAndServe

Fixes #19511

Change-Id: I5585726773b822dba0be0196961132323ebbe084
Reviewed-on: https://go-review.googlesource.com/53071Reviewed-by: 's avatarChris Broadfoot <cbro@golang.org>
parent 915126bb
......@@ -8,6 +8,7 @@ import (
"errors"
"html/template"
"io/ioutil"
"log"
"net/http"
"regexp"
)
......@@ -98,5 +99,5 @@ func main() {
http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler)
http.HandleFunc("/save/", saveHandler)
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
......@@ -7,6 +7,7 @@ package main
import (
"html/template"
"io/ioutil"
"log"
"net/http"
)
......@@ -49,5 +50,5 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
func main() {
http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler)
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
......@@ -7,6 +7,7 @@ package main
import (
"html/template"
"io/ioutil"
"log"
"net/http"
"regexp"
)
......@@ -87,5 +88,5 @@ func main() {
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
......@@ -7,6 +7,7 @@ package main
import (
"html/template"
"io/ioutil"
"log"
"net/http"
)
......@@ -61,5 +62,5 @@ func main() {
http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler)
http.HandleFunc("/save/", saveHandler)
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
......@@ -16,7 +16,7 @@
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
! http.ListenAndServe(":8080", nil)
! log.Fatal(http.ListenAndServe(":8080", nil))
}
--- 87,101 ----
http.HandleFunc("/edit/", makeHandler(editHandler))
......
......@@ -7,6 +7,7 @@ package main
import (
"html/template"
"io/ioutil"
"log"
"net/http"
"regexp"
)
......@@ -85,5 +86,5 @@ func main() {
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
......@@ -2,6 +2,7 @@ package main
import (
"fmt"
"log"
"net/http"
)
......@@ -11,5 +12,5 @@ func handler(w http.ResponseWriter, r *http.Request) {
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
......@@ -213,6 +213,12 @@ worry about its second parameter, <code>nil</code>, for now.)
This function will block until the program is terminated.
</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>
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
......
......@@ -7,6 +7,7 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
......@@ -52,5 +53,5 @@ func editHandler(w http.ResponseWriter, r *http.Request) {
func main() {
http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler)
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
......@@ -7,6 +7,7 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
......@@ -37,5 +38,5 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
func main() {
http.HandleFunc("/view/", viewHandler)
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
......@@ -7,6 +7,7 @@ package main
import (
"html/template"
"io/ioutil"
"log"
"net/http"
)
......@@ -69,5 +70,5 @@ func main() {
http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler)
http.HandleFunc("/save/", saveHandler)
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
......@@ -7,6 +7,7 @@ package main
import (
"html/template"
"io/ioutil"
"log"
"net/http"
)
......@@ -53,5 +54,5 @@ func main() {
http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", editHandler)
//http.HandleFunc("/save/", saveHandler)
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment