Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 静态文件不';从Go开始为他们提供服务时不会更新_Linux_Ubuntu_Go_Vagrant_Ubuntu 16.04 - Fatal编程技术网

Linux 静态文件不';从Go开始为他们提供服务时不会更新

Linux 静态文件不';从Go开始为他们提供服务时不会更新,linux,ubuntu,go,vagrant,ubuntu-16.04,Linux,Ubuntu,Go,Vagrant,Ubuntu 16.04,我开始学习Go,但静态文件处理有问题。 请注意: func main() { fs := http.FileServer(http.Dir("public")) http.Handle("/", fs) err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } } 文件夹结构: main.go public

我开始学习Go,但静态文件处理有问题。 请注意:

func main() {
    fs := http.FileServer(http.Dir("public"))
    http.Handle("/", fs)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}
文件夹结构:

main.go

public
    - index.html
当我运行
go-run-main.go
之后,在
index.html
中更改某些内容,然后再次运行
go-run-main.go
,浏览器中的视图不会更改。所以我在谷歌上搜索了一下,认为它们是go编译的二进制文件,因为
main.go
没有更改,所以go不会重新编译它。所以我运行
go-run-a main.go
来强制重新编译,但它没有帮助

我在chrome中清除历史记录和缓存,甚至尝试其他浏览器和
curl
,但仍然可以看到旧的静态文件,而在文件系统中只有新版本。因此,这与浏览器无关。实际上,一个有效的方法是将
public
重命名为
public2
(例如),当我在浏览器中看到新版本的静态文件时,在
main.go
中进行相同的更改

这不是Go问题,因为这个例子在其他用户中可以正常工作。这就是我的系统。我在Vagrant的默认Ubuntu 16.04上运行了这段代码

流浪汉档案:

# -*- 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
请求标头:

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]
结论:我在另一个虚拟机上运行它,一切正常,所以虚拟机有些问题,但现在我不知道问题出在哪里。

本质上(它不仅如此,但对于我们的目的来说已经足够好了)

我通过复制您提供的main.go来测试您的示例,然后执行以下操作:

go run main.go
# in a new tab
curl localhost:8080
#  ==> "Hello world"
echo "2" >> public/index.html
curl localhost:8080
#  ==> "Hello world\n2"

您不需要重新运行go应用程序,也不需要重新编译go应用程序,除非浏览器中正在进行缓存,否则您应该在刷新页面后看到对index.html所做的更改。请尝试查看index.html是否更改。我刚刚在我的机器上重新创建了你的简单示例,它与firefox配合得很好。你做了一些错误的事情,但Go的http.FileServer的工作方式没有问题。还可以尝试类似的操作,以查看Go是否每次都收到请求,或者只是第一次收到请求。请确保VM中的文件正在更改,否则可能会出现同步/缓存问题。听起来好像有什么东西在缓存它。。。(绝对与golang或您的代码无关)
go run main.go
# in a new tab
curl localhost:8080
#  ==> "Hello world"
echo "2" >> public/index.html
curl localhost:8080
#  ==> "Hello world\n2"