我如何http.ListenAndServe“ /”,而不是对每个路径都做出响应? [重复]
问题描述:
This question already has an answer here:
I have the following code in a small golang server:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {})
Basically allowing you to hit the root of the server without anything scary happening.
The issue is that you can also hit /foo/bar/blah...
and it still works. Which I don't want.
How can I explicitly restrict it to what I say?
</div>
此问题已经存在 在这里有一个答案: p>
-
如何限制用golang编写的Web服务器允许使用特定地址而不是模式?
2个答案
span>
li>
ul>
div> \ r
我在小型golang服务器中具有以下代码: p>
http.HandleFunc(“ /”,func(w http.ResponseWriter,r * http。 Request){}) code> pre>
基本上可以让您访问服务器的根目录而不会发生任何令人恐惧的事情。 p>
问题 是您还可以点击
/ foo / bar / blah ... code>,它仍然可以正常工作。 p>
我该如何明确地将其限制为我所说的话? p> div>
答
Add the following code to the beginning of your handler:
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}