静态目录未得到服务

Static directory not getting served

我正在尝试在我的应用程序的根目录中提供一个名为 assets 的文件夹。我希望可以通过 url /details/.

访问其中的所有文件和子文件夹
fs := http.FileServer(http.Dir("assets"))
http.Handle("/details/", http.StripPrefix("/details/", fs))

我仍然得到 404 的所有内容。我使用 StripPrefix 不正确吗?

为了清楚起见,假设 assets 包含 test.json。我希望可以从 URL /details/test.json.

访问

根据以上评论仔细检查您的路径、权限、用户上下文等

如果您仍然卡住了,请从这个基本设置开始:

package main

import (
        "log"
        "net/http"
)

func main() {
        fs := http.FileServer(http.Dir("/tmp/assets"))
        http.Handle("/details/", http.StripPrefix("/details/", fs))
        log.Fatal(
                http.ListenAndServe(":8080", nil),
        )   
}

并使用 curl 等进行测试

$ find /tmp/assets

/tmp/assets
/tmp/assets/test.json

$ go run ./main.go

$ curl localhost:8080/details/

<pre>
<a href="test.json">test.json</a>
</pre>