gpt4 book ai didi

go - 将 go.rice 与 http.ServeFile() 一起使用

转载 作者:IT王子 更新时间:2023-10-29 01:27:03 28 4
gpt4 key购买 nike

也许我看错了地方,但我找不到将 go.rice 与 http.ServeFile() 结合使用的示例。基本上,我想要的是使用 http.ServeFile() 提供盒装文件。我现在拥有的是以下内容。如您所见,我正在寻找一种方法来获取装箱文件的字符串位置,因为 http.ServeFile 需要这样做。我不知道如何得到它。有什么建议吗?

var StaticBox *rice.Box

func NewStaticBox() {
StaticBox = rice.MustFindBox("../../static")
}

func Static(req *http.Request, resp *http.Response) {
stringToBoxedFile := WHAT-TO-DO-HERE
http.ServeFile(req, resp, stringToBoxedFile)
}

我可以通过多种方式使用 rice.box。我可以使用 StaticBox.String() 等获取文件内容作为字符串。但现在我想要一个字符串作为装箱文件的“位置”。

最佳答案

你可以使用 http.ServeContent反而。获取 HTTPBox并使用 HTTPBox.Open获取实现 io.ReadSeeker 的 http.File,因此它可以与 ServeContent 一起使用。

或者,您可以使用 HTTPBox使用 http.FileServer,如 go.rice 文档中所建议(http.FileServer 将获取请求的路径,并将其用作文件名以在框中查找文件。)

关于go - 将 go.rice 与 http.ServeFile() 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36112133/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com