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

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

在 go 中設置 http 處理程序

在 go 中設置 http 處理程序

Go
HUWWW 2021-08-10 20:42:22
我正在跟蹤 go tour,其中一個練習要求我構建幾個 http 處理程序。這是代碼:    package mainimport (    "fmt"    "net/http")type String stringtype Struct struct {  Greeting string  Punct string  Who string}func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request){    fmt.Fprint(w, s)}func (s *Struct) ServeHTTP(w http.ResponseWriter, r *http.Request){  fmt.Fprint(w, "This is a struct. Yey!")}func main() {    // your http.Handle calls here    http.ListenAndServe("localhost:4000", nil)    http.Handle("/string", String("I'm a frayed knot"))    http.Handle("/struct", &Struct{"Hello",":","Gophers!"})}代碼編譯并運行得很好但是我不確定為什么當我導航到localhost:4000/string或localhost:4000/struct我得到的只是來自默認 http 處理程序的 404 錯誤。我在這里遺漏了一步還是?
查看完整描述

2 回答

?
德瑪西亞99

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

您的代碼停在ListenAndServe,這是阻塞的。(順便說一句,如果ListenAndServe沒有阻塞,main將返回并且進程將退出)

在此之前注冊處理程序。


查看完整回答
反對 回復 2021-08-10
  • 2 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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