第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

Go
慕碼人8056858 2022-01-10 17:01:42
我是一位經(jīng)驗豐富的程序員,但還是新手。如果這是一個明顯的問題或很好的路徑,請?zhí)崆暗狼?。我仍然對語言及其語義有所了解。我正在嘗試在 go 中創(chuàng)建一個 Web 服務(wù)器檢查 HTTP 請求根據(jù)請求的結(jié)果,提供特定的靜態(tài)文件夾即,簡化的偽代碼看起來像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")))      }有經(jīng)驗的 Go 程序員可能已經(jīng)發(fā)現(xiàn)了這個問題。我正在 processRequest 中進行檢查,因此,調(diào)用 Handle 為時已晚——但是,您不能在 go 中為同一路徑注冊多個句柄,并且嵌套句柄調(diào)用異常。我雖然處理程序可能類似于其他語言中的匿名函數(shù)并嘗試調(diào)用它——但 go 也這樣做了。那么 - 有沒有辦法手動調(diào)用從調(diào)用返回的處理程序http.FileServer(http.Dir("./static"))?這是在這里問的正確問題嗎?http 模塊上下文中的處理程序到底是什么?
查看完整描述

1 回答

?
DIEA

TA貢獻1820條經(jīng)驗 獲得超3個贊

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

//編輯

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


查看完整回答
反對 回復(fù) 2022-01-10
  • 1 回答
  • 0 關(guān)注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號