也許我看錯了地方,但我找不到將 go.rice 與http.ServeFile(). 基本上我想要的是提供一個帶有http.ServeFile(). 我現在擁有的是以下內容。正如您所看到的,我正在尋找一種獲取盒裝文件的字符串位置的方法,因為 http.ServeFile 需要這樣做。我不知道如何得到它。有什么建議?var StaticBox *rice.Boxfunc 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(),等等。但現在我想要一個字符串作為盒裝文件的“位置”。
2 回答

夢里花落0921
TA貢獻1772條經驗 獲得超6個贊
您可以改用http.ServeContent。獲取一個HTTPBox,使用 HTTPBox.Open獲取一個http.File,實現io.ReadSeeker,所以可以和ServeContent一起使用。
或者,您可以按照go.rice 文檔中的建議將HTTPBox與http.FileServer一起使用(http.FileServer 將獲取請求的路徑,并將其用作文件名以在框中查找文件。)

吃雞游戲
TA貢獻1829條經驗 獲得超7個贊
實際上有一個非常簡單和簡短的解決方案來實現你想要的:
func main() {
box := rice.MustFindBox("../../static")
http.Handle("/", http.FileServer(box.HTTPBox()))
http.ListenAndServe(":8080", nil)
}
- 2 回答
- 0 關注
- 252 瀏覽
添加回答
舉報
0/150
提交
取消