7Days/day1/base2/main.go

34 lines
656 B
Go
Raw Normal View History

2023-01-24 21:14:53 +08:00
package main
import (
"net/http"
)
type Engine struct{}
func (engine *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2023-02-04 19:00:36 +08:00
path := r.URL.Path
switch path {
2023-01-24 21:14:53 +08:00
case "/":
2023-02-04 19:00:36 +08:00
_, _ = w.Write([]byte("Hello Go"))
case "/gee":
_, _ = w.Write([]byte("Gee!!!"))
2023-01-24 21:14:53 +08:00
default:
2023-02-04 19:00:36 +08:00
_, _ = w.Write([]byte("天啊, 404啦!!!!"))
2023-01-24 21:14:53 +08:00
}
}
2023-02-04 19:00:36 +08:00
2023-01-24 21:14:53 +08:00
func main() {
engine := new(Engine)
http.ListenAndServe(":8000", engine)
}
2023-02-04 19:00:36 +08:00
//// 自定义ServeMux
//func main() {
// mux := new(http.ServeMux)
// mux.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
// _, _ = writer.Write([]byte("Hello Go"))
// })
// _ = http.ListenAndServe(":8000", mux)
//}