Post

Go simple mux

Go simple mux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main

import (
	"io"
	// "html/template"
	"log"
	"net/http"
	// "net/url"
)

// var tpl *template.Template

// func init() {
// 	tpl = template.Must(template.ParseFiles("index.gohtml"))
// }

type pes int

func (p pes) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	if err != nil {
		log.Fatalln(err)
	}

	// data := struct {
	// 	Method         string
	// 	URL            *url.URL
	// 	Submissions    map[string][]string
	// 	Header         http.Header
	// 	Host           string
	// 	ContentLength  int64
	// }{
	// 	Method: r.Method,
	// 	URL: r.URL,
	// 	Submissions: r.Form,
	// 	Header: r.Header,
	// 	Host: r.Host,
	// 	ContentLength: r.ContentLength,
	// }

	// for k, v := range r.Header {
	// 	fmt.Printf("[%v]: %v\n", k, v)
	// }

	// fmt.Println()

	// for k, v := range r.Form {
	// 	fmt.Printf("Form data: [%v]: %v\n", k, v)
	// }

	// w.Header().Set("X-Custom-Header", "this is go")
	// tpl.ExecuteTemplate(w, "index.gohtml", data)

	switch r.URL.Path {
	case "/dog":
		io.WriteString(w, "this is \"/dog\" path")
	case "/cat":
		io.WriteString(w, "this is \"/cat\" path")
	default:
		io.WriteString(w, "No matching /path")

	}


}

func main() {
	var styriPesa pes
	http.ListenAndServe(":8080", styriPesa)
}

This post is licensed under CC BY 4.0 by the author.