【问题标题】:Golang (v1.8) - Error: type *mux.Route has no field or method MethodGolang (v1.8) - 错误:类型 *mux.Route 没有字段或方法 Method
【发布时间】:2017-09-21 18:29:59
【问题描述】:

我有一个配备了 gorilla 工具包的 go/golang 应用程序。我正在尝试利用 gorilla/mux 包进行路由。我的路线和错误消息如下。有什么指点吗?

路线 `

r := mux.NewRouter()
r.HandleFunc("/", landing)
r.HandleFunc("/contact", contact)
r.HandleFunc("/faq", faq)
r.HandleFunc("/register", accountsC.New).Method("GET")
r.HandleFunc("/register", accountsC.Create).Method("POST")
http.ListenAndServe(":8080", r)`

我收到了这个错误:

# command-line-arguments
./main.go:27: r.HandleFunc("/register", accountsC.New).Method undefined 
(type *mux.Route has no field or method Method)
./main.go:28: r.HandleFunc("/register", accountsC.Create).Method undefined 
(type *mux.Route has no field or method Method)

【问题讨论】:

标签: go methods routing gorilla


【解决方案1】:

没有方法Method,需要使用Methods

【讨论】:

    【解决方案2】:

    没有method Method,需要使用Methods 应该是这样的

    r := mux.NewRouter()
    r.HandleFunc("/", landing)
    r.HandleFunc("/contact", contact)
    r.HandleFunc("/faq", faq)
    r.HandleFunc("/register", accountsC.New).Methods("GET")
    r.HandleFunc("/register", accountsC.Create).Methods("POST")
    http.ListenAndServe(":8080", r)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-26
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多