This is a creation in Article, where the information may have evolved or changed.
Today Bo Main research finished things is too late, we can see time one o'clock more, today is code 1, tomorrow is the explanation, we first study
[PHP]
Package Main
Import (
"FMT"
"Html/template"
"Log"
"Net/http"
"OS"
"Time"
)
Const (
Template_dir = "./views/"
)
var mux map[string]func (http. Responsewriter, *http. Request)
Func Main () {
Server: = http. server{
ADDR: ": 9090",
Handler: &myhandler{},
Readtimeout:6 * time. Second,
}
MUX = Make (Map[string]func (HTTP. Responsewriter, *http. Request))
mux["/"] = Home
mux["/login"] = Login
Server. Listenandserve ()
}
Type MyHandler struct{}
Func (*myhandler) servehttp (w http. Responsewriter, R *http. Request) {
If h, OK: = Mux[r.url. String ()]; OK {
H (W, R)
Return
} else {
Defer F.close ()
F, err: = OS. Open ("." + R.url. String ())
If err! = Nil && os. Isnotexist (Err) {
http. NotFound (W, R)
Return
} else {
http. Servefile (W, R, ".") +r.url. String ())
}
Return
}
}
Type index struct {
Title string
}
Func Login (w http. Responsewriter, R *http. Request) {
if R.method = = "GET" {
T, err: = template. Parsefiles (Template_dir + "login.html")
If err! = Nil {
Log. Fatal (ERR)
}
Index: = index{title: "Home"}
T.execute (w, index)
}
}
Func Home (w http. Responsewriter, R *http. Request) {
if R.method = = "GET" {
T, err: = template. Parsefiles (Template_dir + "index.html")
If err! = Nil {
Log. Fatal (ERR)
}
Index: = index{title: "Home"}
T.execute (w, index)
}
}
[/php]
without permission, may not reprint this station any article: the Micro Degree network»golang (go Language) page production instance code 1