Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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
Go web服务器在哪里查找文件_Go - Fatal编程技术网

Go web服务器在哪里查找文件

Go web服务器在哪里查找文件,go,Go,我有一个简单的web应用程序,名为HttpServer.go的代码文件是: package main import ( "net/http" ) func main() { mux := http.NewServeMux() files := http.FileServer(http.Dir("/public")) mux.Handle("/static/", http.StripPrefix("/static/", files)) server :=

我有一个简单的web应用程序,名为HttpServer.go的代码文件是:

package main

import (
    "net/http"
)

func main() {
    mux := http.NewServeMux()
    files := http.FileServer(http.Dir("/public"))
    mux.Handle("/static/", http.StripPrefix("/static/", files))
    server := &http.Server{
        Addr:    "localhost:8080",
        Handler: mux,
    }
    server.ListenAndServe()
}
我已将此代码文件放在%GOPATH%/src/first_app下,我将安装此程序,第一个_app.exe将显示在%GOPATH%/bin中

当我启动Web服务器时,我访问了

,但404NOT FOUND抱怨找不到a.txt


我会问我应该将目录public和a.txt放在哪里,它在表达式中指定的路径中显示/在你的情况下是公开的

很可能您的系统上没有名为/public的路径,因为这是我熟悉的所有操作系统上的非标准目录路径,我怀疑您还没有创建它


更改/public以匹配您放置文件的路径。

相关/可能的重复也可能重复我应该在哪里创建目录/public,它是绝对目录还是相对目录例如,相对于appThank@Flimzy的根目录。我尝试使用绝对目录,如files:=http.FileServerhttp.Dirc:/public。现在它起作用了。但是我想知道我是否可以指定一个相对路径,比如相对于应用程序的rootI我算出了相对路径,如果我使用的是`files:=http.FileServerhttp.Dirpublic,它没有前导/`,它将相对于$GOPATH,因此如果我请求$GOPATH/public/a.txt,它将查找$GOPATH/public/a.txt,就像所有相对路径一样,它相对于当前工作目录,即运行应用程序的位置。