package handle
import (
"html/template"
"net/http"
"utils"
)
var regMap = make(map[string]func(http.ResponseWriter, *http.Request))
regMap["/view"] = func (w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "edit.html", "a")
} // ***这行报:non-declaration statement outside function body***
func Register() {
for url, method := range regMap {
http.HandleFunc(url, method)
}
}
为什么? 等待~
把
regMap["/view"] = func(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "edit.html", "a")
}
放在函数里面。
#2
更多评论
定义一个 init 函数:
func init() {
regMap["/view"] = func (w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "edit.html", "a")
}
}
或者,写成这样:
var regMap = map[string]func(http.ResponseWriter, *http.Request)) {
"/view": func (w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "edit.html", "a")
},
}
#3