I started to learn Go, and have a problem with static files handle. have this:
func main() {
fs := http.FileServer(http.Dir("public"))
http.Handle("/", fs)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
folder structure:
main.go
public
- index.html
When I run go run main.go
and after it, change something in index.html
, and again run go run main.go
, view in browser doesn't changed. So I googled a bit and thought that they are in binary file that go compiles, and because of main.go
wasn't changed, go doesn't recompile it. So I run go run -a main.go
to force recompile, but it doesn't help.
I clear history and cache in chrome and even try another browser and curl
, but still see old static files, while in file system there is only new version. So it is not about browsers. Actually one thing that is working is to rename public
to public2
(for example) and in main.go
make same changes, when I see new version of static files in browser.
It is not Go problem, because this example works OK in other users. So it is something with my system. I run that code on default Ubuntu 16.04 in Vagrant.
vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 5432, host: 5432
end
request header:
2017/11/19 18:25:45 request.RequestURI: /
2017/11/19 18:25:45 request.RemoteAddr: 10.0.2.2:50584
2017/11/19 18:25:45 request.TLS: <nil>
2017/11/19 18:25:45 Request Headers:
2017/11/19 18:25:45 Accept : [text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8]
2017/11/19 18:25:45 Accept-Encoding : [gzip, deflate, br]
2017/11/19 18:25:45 Accept-Language : [en-US,en;q=0.9,ru;q=0.8]
2017/11/19 18:25:45 Cache-Control : [max-age=0]
2017/11/19 18:25:45 Connection : [keep-alive]
2017/11/19 18:25:45 If-Modified-Since : [Sun, 19 Nov 2017 16:24:53 GMT]
2017/11/19 18:25:45 Upgrade-Insecure-Requests : [1]
2017/11/19 18:25:45 User-Agent : [Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36]
response header:
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 2010
Content-Type: text/html; charset=utf-8
Last-Modified: Sun, 19 Nov 2017 16:24:53 GMT
Date: Sun, 19 Nov 2017 18:25:27 GMT
CONCLUSION: I run that on another vm and everything work fine, so there is something about vm, but now I don't know what is the problem.
go run essentially builds a binary, copies it to a tmp folder and then executes it (its more than that, but good enough for our purposes).
I tested out your example by copying the main.go you provided then doing the following:
go run main.go
# in a new tab
curl localhost:8080
# ==> "Hello world"
echo "2" >> public/index.html
curl localhost:8080
# ==> "Hello world
2"