【Go/golang】原生监听处理http请求的写法
原文地址:https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/03.4.md
由于自己经常忘了怎么写,而Github又经常抽风,所以这里做个备份
package main
import (
"fmt"
"net/http"
)
type MyMux struct {
}
func (p *MyMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
sayhelloName(w, r)
return
}
http.NotFound(w, r)
return
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello myroute!")
}
func main() {
mux := &MyMux{}
http.ListenAndServe(":9090", mux)
}