3 回答

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
也許這可以幫助你:
type MyHandler struct{}
func (self MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
http.ListenAndServe(":3000", &MyHandler{})

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
這是翻譯成 Go 的問(wèn)題中的 node.js 代碼。Go 代碼與 node.js 代碼非常相似。
func myHandler(response http.ResponseWriter, request *http.Request) {
switch request.Method {
case "OPTIONS":
OptionsResponse(response, request)
case "GET":
GetSwitch(response, request)
case "POST":
PostSwitch(response, request)
case "PUT":
PutSwitch(response, request)
case "DELETE":
DeleteSwitch(response, request)
default:
http.Error(response, "Sorry, this method not supported", 501)
}
}

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊
我認(rèn)為您需要像這樣為您的代碼編寫響應(yīng)和請(qǐng)求。因?yàn)槭钦Z(yǔ)言的默認(rèn)包
func myHandler(w http.ResponseWriter, r *http.Request) {
switch request.Method {
case "OPTIONS":
OptionsResponse(response, request)
case "GET":
GetSwitch(response, request)
case "POST":
PostSwitch(response, request)
case "PUT":
PutSwitch(response, request)
case "DELETE":
DeleteSwitch(response, request)
default:
http.Error(response, "Sorry, this method not supported", 501)
}
}
- 3 回答
- 0 關(guān)注
- 99 瀏覽
添加回答
舉報(bào)