-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp
84 lines (68 loc) · 1.46 KB
/
temp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*package main
import (
"io"
"log"
"net/http"
"time"
)
/*
func main(){
// 设置路由
http.HandleFunc("/", sayHello)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
func sayHello(w http.ResponseWriter, r *http.Request){
io.WriteString(w, "this is a Hello work")
}
*/
/*
func main() {
mux := http.NewServeMux()
mux.Handle("/", &myHandler{})
mux.HandleFunc("/hello", sayHello)
err := http.ListenAndServe(":8080", mux)
if err != nil {
log.Fatal(err)
}
}
type myHandler struct {}
func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request){
io.WriteString(w, "URL:" + r.URL.String())
}
func sayHello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "this is a Hello version2")
}
*/
/*
var mux map[string]func(http.ResponseWriter, *http.Request)
func main(){
server := http.Server{
Addr: ":8080",
Handler: &myHandler{},
ReadTimeout: 5 * time.Second,
}
mux = make(map[string]func(http.ResponseWriter, *http.Request))
mux["/hello"] = sayHello
mux["/bye"] = sayBye
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
type myHandler struct {}
func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request){
if h, ok := mux[r.URL.String()]; ok {
h(w, r)
return
}
}
func sayHello(w http.ResponseWriter, r *http.Request){
io.WriteString(w, "this is a hello version 4")
}
func sayBye(w http.ResponseWriter, r *http.Request){
io.WriteString(w, "this is a bye version 4")
}
*/