-1

I run the code

package main

import (
    "fmt"
    "net/http"
)

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

func sroot(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Welcome")
}

and browser showed a response as expected: Welcome then, after some time I try to change the output but have found that the output does not change! so, when I change the output fmt.Fprintf(w, "Welcome 123") but browser still ouptuts Welcome.

So what's the magic happens here?

user8049659
  • 129
  • 1
  • 1
  • 8
  • 1
    did you restart the server after changes? – saddam Jan 02 '19 at 12:36
  • I restarted a program, but how to restart a server? And I run it from IDE. – user8049659 Jan 02 '19 at 12:37
  • which one IDE are you using ? – saddam Jan 02 '19 at 12:39
  • @saddam I'm using LiteIDE and I run the file via Alt+F6 – user8049659 Jan 02 '19 at 12:41
  • @rɑːdʒɑ updated an answer, follow that. – saddam Jan 02 '19 at 12:44
  • @saddam seems it doesn't work – user8049659 Jan 02 '19 at 13:01
  • The answer of @dan-esparza is likely to be correct, you can check this question to see how to debug if the browser used cache or not in chrome: https://stackoverflow.com/questions/13140318/check-whether-network-response-is-coming-from-server-or-chrome-cache – thst Jan 02 '19 at 13:01
  • @thst disabling a browser cache does not solve the issue, but I was able to run it with different port adress, but againe if I want to change the output even with the new port adress the response does not change. – user8049659 Jan 02 '19 at 13:14
  • @user8049659 there would be a refresh icon on IDE which will restart the server, try it. – saddam Jan 02 '19 at 13:21
  • @user8049659 I tried your code and it works for me in goland. Add a line `log.Print("received call")` in your `sroot` func. This will tell you if there is a cache issue or something other. – thst Jan 02 '19 at 13:36
  • @thst Added it before fmt.Fprintf... restarted, then changed response and again restarted - nothing changed – user8049659 Jan 02 '19 at 13:44
  • You should see the log message for each request. like this `2019/01/02 14:39:05 received call` – thst Jan 02 '19 at 13:45

2 Answers2

2

By default the browser makes a GET request when navigating to the page. The browser is also going to make some decisions about the 'cachability' of the page: Has the url changed? Has the querystring changed? Has the ETAG changed? If none of these are true, the browser is most likely serving a cached version of the page.

Dan Esparza
  • 28,047
  • 29
  • 99
  • 127
  • ETag doesn't work like that. The browser doesn't know if the ETag has changed until it makes another request, which will include If-None-Match if the ETag is known. The *server* then gets to decide if it returns 304 Not Modified or new content. Also cacheability is controlled by Cache-Control and Expires. – Adrian Jan 02 '19 at 15:11
0

You can use gin to reload your webserver.

Installation: go get github.com/codegangsta/gin

Usage: gin run filename.go

Reference: https://github.com/codegangsta/gin

Raja G
  • 5,973
  • 14
  • 49
  • 82
  • Installed it, created folder in `src` named `webserver` and there is a main file, so then I call it `gin run webserver.go` it shows me `[gin] Listening on port 3000 [gin] Building... [gin] ←[97;32;1mBuild finished←[0m` but the browser output is the same as was before even after reloading the browser tab. – user8049659 Jan 02 '19 at 12:57