也許我看錯了地方,但我找不到將 go.rice 與http.ServeFile(). 基本上我想要的是提供一個帶有http.ServeFile(). 我現(xiàn)在擁有的是以下內(nèi)容。正如您所看到的,我正在尋找一種獲取盒裝文件的字符串位置的方法,因?yàn)?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。我可以將文件內(nèi)容作為字符串獲取StaticBox.String(),等等。但現(xiàn)在我想要一個字符串作為盒裝文件的“位置”。
2 回答

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

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