Golang的handlefunc函数

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

Golang handlefunc

问题

所以我有一个奇怪的问题,其中一些路由可以工作,而其他路由则不行。我首先会展示给你我的主要函数,然后给出问题的示例。

func main() {
	http.HandleFunc("/", home)
	http.HandleFunc("/project", project)
	http.HandleFunc("/about", about)
	http.HandleFunc("/contact", contact)

	http.Handle("/resources/", http.StripPrefix("/resources", http.FileServer(http.Dir("./assets"))))

	err := http.ListenAndServe(":80", nil)
	if err != nil {
		fmt.Println("ListendAndServe doesn't work: ", err)
	}
}

例如,路由"/contact/"不起作用,当我运行这段代码并访问localhost/contact时,它会将我发送到主页。然而,当我将Handlefunc中的路由更改为"/contactos",然后访问localhost/contactos时,它确实起作用。

另一个例子,"/project"现在可以工作,但当我将其更改为"/projects"时,它就不起作用了。

英文:

So I have this weird issue where some routes will work and others won't. I will first show you my main function and then give examples of what the problem is.

func main() {
	http.HandleFunc("/", home)
	http.HandleFunc("/project", project)
	http.HandleFunc("/about", about)
	http.HandleFunc("/contact", contact)

	http.Handle("/resources/", http.StripPrefix("/resources", http.FileServer(http.Dir("./assets"))))

	err := http.ListenAndServe(":80", nil)
	if err != nil {
		fmt.Println("ListendAndServe doesn't work : ", err)
	}
}

For example, the route "/contact/" does not work, when I run this code and go to localhost/contact it will send me to the homepage. However when I change the route in the Handlefunc to "/contactos" and then go to localhost/contactos it does work.

Another example, "/project" works now, but when I change it to "/projects" it does not.

答案1

得分: 2

请注意,如果你注册了/project/(注意末尾的斜杠),那么/project//project都可以使用(带或不带斜杠)。如果你注册了/project(没有末尾的斜杠),那么只有/project可以使用,/project/将会被根处理器/匹配。

引用自http.ServeMux

> 如果已经注册了一个子树,并且收到了一个不带末尾斜杠的子树根的请求,ServeMux会将该请求重定向到子树根(添加末尾斜杠)。 这个行为可以通过单独为不带末尾斜杠的路径注册来覆盖。例如,注册"/images/"会导致ServeMux将对"/images"的请求重定向到"/images/",除非"/images"已经单独注册。

参考相关问题:

https://stackoverflow.com/questions/60159221/how-to-map-to-the-same-function-with-a-pattern-that-ends-with-or-with-with-h/60184270#60184270

英文:

Note that if you register /project/ (note the trailing slash), then both /project/ and /project will work (with or without trailing slash). If you register /project (without a trailing slash), then only /project will work, /project/ will be matched by the root handler /.

Quoting from http.ServeMux:

> If a subtree has been registered and a request is received naming the subtree root without its trailing slash, ServeMux redirects that request to the subtree root (adding the trailing slash). This behavior can be overridden with a separate registration for the path without the trailing slash. For example, registering "/images/" causes ServeMux to redirect a request for "/images" to "/images/", unless "/images" has been registered separately.

See related questions:

https://stackoverflow.com/questions/60159221/how-to-map-to-the-same-function-with-a-pattern-that-ends-with-or-with-with-h/60184270#60184270

huangapple
  • 本文由 发表于 2021年10月7日 21:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/69482413.html
匿名

发表评论

匿名网友

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

确定