亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在進行中,手動調用 http.FileServer 和/或什么是 http 處理程序

在進行中,手動調用 http.FileServer 和/或什么是 http 處理程序

Go
慕碼人8056858 2022-01-10 17:01:42
我是一位經驗豐富的程序員,但還是新手。如果這是一個明顯的問題或很好的路徑,請提前道歉。我仍然對語言及其語義有所了解。我正在嘗試在 go 中創建一個 Web 服務器檢查 HTTP 請求根據請求的結果,提供特定的靜態文件夾即,簡化的偽代碼看起來像import (    "io"    "net/http"    "fmt"    "strings"    "encoding/base64")func examineRequest(request *http.Request) {    //looks at request header    if(request headers have one thing){        return "foo"    }    return "bar"}func processRequest(responseWriter http.ResponseWriter, request *http.Request) {    folderToServe = examineRequest(request);    if folderToServe == "bar" {        //serve static files from the ./static/bar folder        //go freaks out if I try these        //http.Handle("/", http.FileServer(http.Dir("./static/bar")))              //http.FileServer(http.Dir("./static/bar")()    }    else if folderToServer == "foo" {        //serve static files from the ./static/foo folder        //go freaks out if I try these        //http.Handle("/", http.FileServer(http.Dir("./static/foo")))              //http.FileServer(http.Dir("./static/foo")()    }}func main(){    http.HandleFunc("/", processRequest)      //http.Handle("/", http.FileServer(http.Dir("./static")))      }有經驗的 Go 程序員可能已經發現了這個問題。我正在 processRequest 中進行檢查,因此,調用 Handle 為時已晚——但是,您不能在 go 中為同一路徑注冊多個句柄,并且嵌套句柄調用異常。我雖然處理程序可能類似于其他語言中的匿名函數并嘗試調用它——但 go 也這樣做了。那么 - 有沒有辦法手動調用從調用返回的處理程序http.FileServer(http.Dir("./static"))?這是在這里問的正確問題嗎?http 模塊上下文中的處理程序到底是什么?
查看完整描述

1 回答

?
DIEA

TA貢獻1820條經驗 獲得超3個贊

使用http.FileServer(http.Dir("./static/foo")).ServeHTTP(w, req).

//編輯

http.FileServer返回一個http.Handler依次提供ServerHTTP方法的。


查看完整回答
反對 回復 2022-01-10
  • 1 回答
  • 0 關注
  • 225 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號