From 58aad49347b0791a7d970720248f98bfc4516d04 Mon Sep 17 00:00:00 2001 From: Pmanu96 <37467930+Pmanu96@users.noreply.github.com> Date: Sat, 17 Mar 2018 15:15:03 +0530 Subject: [PATCH] Create task3 --- Submissions/task3.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Submissions/task3.go diff --git a/Submissions/task3.go b/Submissions/task3.go new file mode 100644 index 0000000..acd4902 --- /dev/null +++ b/Submissions/task3.go @@ -0,0 +1,35 @@ +package main + +import ( + "io" + "net/http" +) + +func hello(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "Hello world!") +} + +var mux map[string]func(http.ResponseWriter, *http.Request) + +func main() { + server := http.Server{ + Addr: ":8000", + Handler: &myHandler{}, + } + + mux = make(map[string]func(http.ResponseWriter, *http.Request)) + mux["/"] = hello + + server.ListenAndServe() +} + +type myHandler struct{} + +func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if h, ok := mux[r.URL.String()]; ok { + h(w, r) + return + } + + io.WriteString(w, "My server: "+r.URL.String()) +}