From 500c147acbe6138828efe90aa457317a8101eb7b Mon Sep 17 00:00:00 2001 From: xiaowei Date: Sun, 13 Feb 2022 10:34:46 +0800 Subject: [PATCH] =?UTF-8?q?add:=20web=E5=9F=BA=E6=9C=AC=E7=9F=A5=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/framework/basic/main.go | 26 ++++++++++++++++++++++++++ web/framework/hander-basic/main.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 web/framework/basic/main.go create mode 100644 web/framework/hander-basic/main.go diff --git a/web/framework/basic/main.go b/web/framework/basic/main.go new file mode 100644 index 0000000..9d9aff7 --- /dev/null +++ b/web/framework/basic/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "net/http" +) + +func main() { + + // 路由1 + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "hello basic web!") + }) + + // 路由2 + http.HandleFunc("/xiaowei", func(w http.ResponseWriter, r *http.Request) { + headers := r.Header + for k, v := range headers { + fmt.Fprintf(w, "[%v]-[%v]\n", k, v) + } + }) + // 启动一个httpserver + if err := http.ListenAndServe(":9000", nil); err != nil { + panic(err) + } +} diff --git a/web/framework/hander-basic/main.go b/web/framework/hander-basic/main.go new file mode 100644 index 0000000..6526dd2 --- /dev/null +++ b/web/framework/hander-basic/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "net/http" +) + +// Engine 定义一个结构体 +type Engine struct { +} + +// 实现Handler的ServerHTTP方法 +func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) { + switch r.RequestURI { + case "/": + fmt.Fprint(w, "hello handler-basic") + case "/web": + for k, v := range r.Header { + fmt.Fprintln(w, k, "-->", v) + } + } +} + +func main() { + engine := new(Engine) + if err := http.ListenAndServe(":9000", engine); err != nil { + panic(err) + } +}