Why Handler registered by http.HandleFunc is called twice?

5,195 views
Skip to first unread message

kawamoto

unread,
Mar 22, 2013, 7:14:24 AM3/22/13
to golang-nuts
In the below code, "hello" is called twice when I accessed to
http://localhost:8080. why?

package main

import (
"fmt"
"net/http"
)

func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println("hello")
}

Dave Cheney

unread,
Mar 22, 2013, 7:17:45 AM3/22/13
to kawamoto, golang-nuts
Your browser is requesting /favicon.ico, this matches the catchall
handler, "/" so you get called twice.
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Scott Lawrence

unread,
Mar 22, 2013, 7:14:36 AM3/22/13
to kawamoto, golang-nuts
If you log the request, you'll see your browser is probably also requesting
/favicon.ico, which is being passed to that same handler.
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

--
Scott Lawrence

go version go1.0.3
Linux baidar 3.8.3-2-ARCH #1 SMP PREEMPT Sun Mar 17 13:04:22 CET 2013 x86_64 GNU/Linux

Jan Mercl

unread,
Mar 22, 2013, 7:20:16 AM3/22/13
to Dave Cheney, kawamoto, golang-nuts
On Fri, Mar 22, 2013 at 12:17 PM, Dave Cheney <da...@cheney.net> wrote:
> Your browser is requesting /favicon.ico, this matches the catchall
> handler, "/" so you get called twice.

I think this is a FAQ candidate.

-j

Yasutaka Kawamoto

unread,
Mar 22, 2013, 7:57:21 AM3/22/13
to Scott Lawrence, golang-nuts
Thanks All

I got.

I use Google Chrome.
How do you avoid it?


2013/3/22 Scott Lawrence <byt...@gmail.com>:

nvcnvn

unread,
Mar 22, 2013, 8:14:57 AM3/22/13
to golan...@googlegroups.com, Scott Lawrence
func main() { 
        http.HandleFunc("/favicon.ico", handlerICon) 
        http.HandleFunc("/", handler) 
        http.ListenAndServe(":8080", nil) 


Vào 18:57:21 UTC+7 Thứ sáu, ngày 22 tháng ba năm 2013, kawamoto đã viết:

Yasutaka Kawamoto

unread,
Mar 22, 2013, 8:27:32 AM3/22/13
to nvcnvn, golan...@googlegroups.com, Scott Lawrence
Oh... I see.
I could run in below code.

package main

import (
"fmt"
"net/http"
)

func main() {
http.HandleFunc("/favicon.ico", handlerICon)
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)

}

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println("hello")
}

func handlerICon(w http.ResponseWriter, r *http.Request) {}


Thanks!


2013/3/22 nvcnvn <nvc...@gmail.com>:

Jeremy Wall

unread,
Mar 22, 2013, 12:11:53 PM3/22/13
to Yasutaka Kawamoto, Scott Lawrence, golang-nuts
if you just want to verify it only gets called once you can always hit the url with curl which won't generate extra http requrests for you.

Yasutaka Kawamoto

unread,
Mar 22, 2013, 12:47:55 PM3/22/13
to Jeremy Wall, Scott Lawrence, golang-nuts
I got it.

When I don't write "http.HandleFunc("/favicon.ico", handlerICon)" and
I hit the url with curl,
I checked it only gets once.

Thank you for the information.

2013/3/23 Jeremy Wall <jw...@google.com>:

Jason

unread,
Mar 25, 2013, 2:54:55 PM3/25/13
to golan...@googlegroups.com, Jeremy Wall, Scott Lawrence
I have a meta-question: Why is "/" a catch-all URL instead of "" (or almost anything else)? This also bit me when I wrote a "/" handler, and I expect it's common for someone to write a specific handler for their HTTP server home page. There doesn't seem to be a way to specify that special handler, though.

atomly

unread,
Mar 25, 2013, 2:57:50 PM3/25/13
to Jason, golang-nuts, Jeremy Wall, Scott Lawrence
On Mon, Mar 25, 2013 at 2:54 PM, Jason <jki...@gmail.com> wrote:
I have a meta-question: Why is "/" a catch-all URL instead of "" (or almost anything else)? This also bit me when I wrote a "/" handler, and I expect it's common for someone to write a specific handler for their HTTP server home page. There doesn't seem to be a way to specify that special handler, though.

Yeah, it feels like it should be "/*" or "*" or something, with "/" specifically referring to "/" and only "/"... 

:: atomly ::

[ ato...@atomly.com : www.atomly.com  : http://blog.atomly.com/ ...
[ atomiq records : new york city : +1.347.692.8661 ...
[ e-mail atomly-new...@atomly.com for atomly info and updates ...

Jason

unread,
Mar 25, 2013, 8:12:30 PM3/25/13
to golan...@googlegroups.com, Jeremy Wall, Scott Lawrence, kara...@gmail.com
Well, sure, but that specifies a resource (the root resource). Why should it stand in for:

/
/favicon.ico
/foo/bar

etc.?

On Monday, March 25, 2013 3:00:37 PM UTC-4, kara...@gmail.com wrote:
Because when you request a page in http the minimum request is : GET / HTTP/1.1
IMHO, since / is the root of your server in http, they used that naming for the handler too.

Liigo Zhuang

unread,
Mar 26, 2013, 4:06:49 AM3/26/13
to atomly, golang-nuts, Jason, Scott Lawrence, Jeremy Wall

+1

Brad Fitzpatrick

unread,
Mar 26, 2013, 11:44:50 AM3/26/13
to Jason, golan...@googlegroups.com, Jeremy Wall, Scott Lawrence
Reply all
Reply to author
Forward
0 new messages