HandleFunc being called twice

后端 未结 3 1161
醉话见心
醉话见心 2020-11-29 12:36

I am following a simple web server example in Go.

I inserted a log statement so that the resulting code looks like below:

package main

         


        
相关标签:
3条回答
  • 2020-11-29 12:50

    As Didier says, it's your browser trying to load the favicon. You can prevent this by adding this code to the <head>:

    <link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=">
    
    0 讨论(0)
  • 2020-11-29 12:54

    Just log the requests. You will realize that your browser also requests /favicon.ico.

    See https://en.wikipedia.org/wiki/Favicon for more information.

    0 讨论(0)
  • 2020-11-29 13:02

    If you request from a service is fine, but if you test from a browser and is not the production intention you can do this in golang code for avoid browser load the favicon:

    http.HandleFunc("/favicon.ico", doNothing)
    

    And the function

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

    For people voting negatives: this works, sorry for handle it with this code in an api that doesn't need favicon or whatever...

    0 讨论(0)
提交回复
热议问题