|  | 
|  | 1 | +package main | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"io" | 
|  | 5 | +	"net/http" | 
|  | 6 | +	"time" | 
|  | 7 | +) | 
|  | 8 | + | 
|  | 9 | +var mux map[string]func(http.ResponseWriter, *http.Request) | 
|  | 10 | + | 
|  | 11 | +func main() { | 
|  | 12 | +	server := http.Server{ | 
|  | 13 | +		Addr: ":8080", | 
|  | 14 | +		Handler: &MyHandler{}, | 
|  | 15 | +		ReadTimeout: 6 * time.Second, | 
|  | 16 | +	} | 
|  | 17 | +	mux = make(map[string]func(http.ResponseWriter, *http.Request)) | 
|  | 18 | +	mux["/hello"] = hello | 
|  | 19 | +	mux["/bye"] = bye | 
|  | 20 | + | 
|  | 21 | +	server.ListenAndServe() | 
|  | 22 | +} | 
|  | 23 | + | 
|  | 24 | +type MyHandler struct{} | 
|  | 25 | + | 
|  | 26 | +func (*MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | 
|  | 27 | +	if h, ok := mux[r.URL.String()]; ok { | 
|  | 28 | +		h(w, r) | 
|  | 29 | +	} | 
|  | 30 | +	io.WriteString(w, "\nURL: "+r.URL.String()) | 
|  | 31 | +} | 
|  | 32 | + | 
|  | 33 | +func hello(w http.ResponseWriter, r *http.Request) { | 
|  | 34 | +	io.WriteString(w, "hello module") | 
|  | 35 | +} | 
|  | 36 | + | 
|  | 37 | +func bye(w http.ResponseWriter, r *http.Request) { | 
|  | 38 | +	io.WriteString(w, "bye module") | 
|  | 39 | +} | 
0 commit comments