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 ( ...@@ -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() { ...@@ -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))
} }
...@@ -7,6 +7,7 @@ package main ...@@ -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) { ...@@ -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))
} }
...@@ -7,6 +7,7 @@ package main ...@@ -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() { ...@@ -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))
} }
...@@ -7,6 +7,7 @@ package main ...@@ -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() { ...@@ -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))
} }
...@@ -16,7 +16,7 @@ ...@@ -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))
......
...@@ -7,6 +7,7 @@ package main ...@@ -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() { ...@@ -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))
} }
...@@ -2,6 +2,7 @@ package main ...@@ -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) { ...@@ -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))
} }
...@@ -213,6 +213,12 @@ worry about its second parameter, <code>nil</code>, for now.) ...@@ -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
......
...@@ -7,6 +7,7 @@ package main ...@@ -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) { ...@@ -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))
} }
...@@ -7,6 +7,7 @@ package main ...@@ -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) { ...@@ -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))
} }
...@@ -7,6 +7,7 @@ package main ...@@ -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() { ...@@ -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))
} }
...@@ -7,6 +7,7 @@ package main ...@@ -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() { ...@@ -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))
} }
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