Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
37 lines (31 sloc) 822 Bytes
// Seriál "Programovací jazyk Go"
//
// Dvanáctá část
//
// Demonstrační příklad číslo 6:
// Využití uzávěrů pro zápis kratšího kódu
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func sendStaticPage(writer http.ResponseWriter, filename string) {
body, err := ioutil.ReadFile(filename)
if err == nil {
fmt.Fprint(writer, string(body))
} else {
writer.WriteHeader(http.StatusNotFound)
fmt.Fprint(writer, "Not found!")
}
}
func staticPage(filename string) func(writer http.ResponseWriter, request *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
sendStaticPage(writer, filename)
}
}
func main() {
http.HandleFunc("/", staticPage("index.html"))
http.HandleFunc("/missing", staticPage("missing.html"))
http.ListenAndServe(":8000", nil)
}
You can’t perform that action at this time.