我是一位經驗豐富的程序員,但還是新手。如果這是一個明顯的問題或很好的路徑,請提前道歉。我仍然對語言及其語義有所了解。我正在嘗試在 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 模塊上下文中的處理程序到底是什么?
在進行中,手動調用 http.FileServer 和/或什么是 http 處理程序
慕碼人8056858
2022-01-10 17:01:42