我正在跟踪 go tour,其中一个练习要求我构建几个 http 处理程序。这是代码:
package main
import (
"fmt"
"net/http"
)
type String string
type 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 错误。
我在这里遗漏了一步还是?
德玛西亚99
相关分类