-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (36 loc) · 944 Bytes
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
// Index is a basic HTTP Handler.
func Index(w http.ResponseWriter, r *http.Request) {
m := map[string]string{
"Hello": "World",
"Hi": "Berlin",
}
w.Header().Set("Content-Type", "application/json")
b, err := json.Marshal(m)
if err != nil {
log.Println(err)
}
w.Write(b)
// fmt.Fprintf(w, "Welcome")
}
// Echo is a basic HTTP Handler.
func Echo(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Fprintf(w, "You asked to %s %s\n", r.Method, r.URL.Path)
}
func main() {
log.Println("http://localhost:8000")
// Handle registers the handler for the given pattern in the DefaultServeMux.
http.HandleFunc("/", Index)
http.HandleFunc("/echo", Echo)
// Start a server listening on port 8000 and responding using Echo.
if err := http.ListenAndServe("localhost:8000", nil); err != nil {
log.Fatalf("error: listening and serving: %s", err)
}
}