1. 首页
  2. 主题
  3. Go Web开发

报错:non-declaration statement outside function body

milo · · 13188 次点击
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) } } 为什么? 等待〜
David
Life is hard but please enjoy it!
定义一个 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
更多评论
gofmt运行此代码,报:`expected declaration, found 'IDENT' regMap`
#1
polaris
社区,需要你我一同完善!
把 regMap["/view"] = func(w http.ResponseWriter, r *http.Request) { renderTemplate(w, "edit.html", "a") } 放在函数里面。
#2

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中