1,常规教程使用,实际中基本不会这么用
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, err := writer.Write([]byte("hello world"))
if err != nil {
panic(err)
}
})
err := http.ListenAndServe(":9090", nil)
if err != nil {
panic(err)
}
fmt.Println("监听了9090端口")
2,实际使用
type MyMux struct {}
func (m *MyMux) ServeHTTP(w http.ResponseWriter, r *http.Request){
if r.URL.Path == "/" {
_, err := fmt.Fprintln(w, "hello world it me")
if err != nil {
panic(err)
}
} else {
http.NotFound(w,r)
}
return
}
func Server2(){
mux := &MyMux{}
err := http.ListenAndServe(":9091", mux)
if err != nil {
panic(err)
}
}
网友评论