-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.go
45 lines (35 loc) · 891 Bytes
/
web.go
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
// web 服务器
package main
//包 http 通过任何实现了 http.Handler 的值来响应 HTTP 请求
/*
type Handler interface {
ServeHTTP(w ResponseWriter, r *Request)
}
*/
import (
"fmt"
"net/http"
)
type hello struct{}
func (h *hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello!")
}
func web() {
var h hello
http.ListenAndServe("localhost:4000", &h)
}
type String string
type Struct struct {
Greeting, Punct, Who string
}
func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, s)
}
func (str *Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, str.Greeting)
}
func web_route() {
http.Handle("/string", String("I'm a frayed knot."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
http.ListenAndServe("localhost:4000", nil)
}