我有一个带有FileServer函数的Shell,我希望能够更改Golang目录。这就是我所拥有的:
func Server() {
wdir, _ := os.Getwd()
m := http.NewServeMux()
fs := http.FileServer(http.Dir(wdir))
m.Handle("/", http.StripPrefix("/", fs))
s := http.Server{Addr: ":8000", Handler: m}
m.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
s.Shutdown(context.Background())
})
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
log.Printf("Finished")
}我这样做,这样如果我到达shutdown,FileServer就会关闭。但是,无论我从哪条路径启动Server(),路径始终保持不变。
因此,如果我从shell cd到另一个文件夹并启动Server(),我仍然会从可执行文件所在的目录中获取文件。
我想要的是,如果我从/home/user/Downloads/shell运行shell,并且在shell中通过cd访问/opt,那么在服务器中显示的文件就是/opt格式的文件,而不是/home/user/Downloads/shell格式的文件
发布于 2019-05-15 01:10:21
您可以引入FileServer path作为标志,并使用它来创建FileServer。
http.FileServer(http.Dir(path + "/opt")).请检查go提供的标志library。
在终端中,您可以将pwd输入到该标志值
https://stackoverflow.com/questions/56132743
复制相似问题