fmt print hello two times when accesing to the server

138 views
Skip to first unread message

WALID BELRHALMIA

unread,
Aug 31, 2015, 3:26:20 PM8/31/15
to golang-nuts
here is my code : 

func main() {
 http
.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 w
.Header().Add("Content Type", "text/html")
 templ
:= template.New("test")


 _
, er := templ.Parse(doc)


 
if er == nil {

 fmt
.Print("hello")   // Here hello print two times in the console

 
}
 
})


 http
.ListenAndServe(":8080", nil)


}

why when i access to the server fmt print hello two times

David Anderson

unread,
Aug 31, 2015, 3:28:05 PM8/31/15
to WALID BELRHALMIA, golang-nuts
Browsers load /favicon.ico automatically. That's the second request.

- Dave

--
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/d/optout.

James Bardin

unread,
Aug 31, 2015, 3:29:31 PM8/31/15
to golang-nuts
Print the path being called to see what it is.
You're probably testing with a browser which will look for a favicon.

WALID BELRHALMIA

unread,
Aug 31, 2015, 4:59:32 PM8/31/15
to golang-nuts

- ok i understand that so why one time the browser make the second request for it and one time it didn't 

- and why when i put a image favicon.ico in my root project 16*16 my two navgator google and firefox don't load it

James Bardin

unread,
Aug 31, 2015, 5:13:54 PM8/31/15
to WALID BELRHALMIA, golang-nuts
On Mon, Aug 31, 2015 at 4:59 PM, WALID BELRHALMIA <wbelr...@gmail.com> wrote:
>
> - ok i understand that so why one time the browser make the second request for it and one time it didn't
>

The browser cached the favicon response.

> - and why when i put a image favicon.ico in my root project 16*16 my two navgator google and firefox don't load it
>

If you're using the code you posted, you're not serving any files, so
the browsers have no way of loading them.

WALID BELRHALMIA

unread,
Aug 31, 2015, 5:17:55 PM8/31/15
to golang-nuts
@james  how can i serve favicon.ico to the web browser i think browser atuomaticly search fro favicon.ico in the root project

Kiki Sugiaman

unread,
Sep 1, 2015, 3:29:35 AM9/1/15
to golan...@googlegroups.com
http://golang.org/pkg/net/http/#FileServer

You could put your favicon file in the same directory as your other
static assets and redirect /favicon.ico requests into
/urlpath/to/your/other/assets/favicon.ico


On 01/09/15 07:17, WALID BELRHALMIA wrote:
> @james how can i serve favicon.ico to the web browser i think browser
> atuomaticly search fro favicon.ico in the root project
> --
> 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
> <mailto:golang-nuts...@googlegroups.com>.

Val

unread,
Sep 1, 2015, 5:50:20 AM9/1/15
to golang-nuts
Hi Walid, this page may help you with the code needed to serve static files and/or a dynamic homepage :
http://stackoverflow.com/questions/14086063/serve-homepage-and-static-content-from-root
Reply all
Reply to author
Forward
0 new messages