Golang(v1.8)- 错误:类型 *mux.Route 没有字段或方法 Method

huangapple go评论77阅读模式
英文:

Golang (v1.8) - Error: type *mux.Route has no field or method Method

问题

我有一个使用gorilla工具包的Go/Golang应用程序。我正在尝试使用gorilla/mux包进行路由。下面是我的路由和错误信息。有什么建议吗?

路由部分代码如下:

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)

我收到了以下错误信息:

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

I have a go/golang application equipped the gorilla toolkit. I am trying to utilize the gorilla/mux package for routing. My routes and the error message are below. Any pointers?

Routes
`

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)`

I have received this error:

# 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)

答案1

得分: 1

没有Method方法,你需要使用Methods

英文:

There is no method Method, you need to use Methods

答案2

得分: 0

没有方法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)

请注意,这是一段Go语言代码,用于创建一个基本的HTTP服务器。它使用了一个名为mux的路由器来处理不同的URL路径,并将它们映射到相应的处理函数上。其中Methods函数用于指定处理函数可以处理的HTTP请求方法。最后,http.ListenAndServe函数用于启动服务器并监听指定的端口(这里是8080)。

英文:

There is no method Method, you need to use Methods
should be like that

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)

huangapple
  • 本文由 发表于 2017年4月25日 06:46:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/43598861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定