-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (55 loc) · 1.83 KB
/
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
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
72
73
74
package main
import (
"fmt"
"net/http"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5"
)
func checkURL(w http.ResponseWriter, r *http.Request) {
//w.Write([]byte("Hello World!!!!"))
keyParam := chi.URLParam(r, "key")
//w.Write([]byte(keyParam))
if keyParam == "hello" {
w.Write([]byte("World!"))
} else if keyParam == "world" {
w.Write([]byte("Hello!"))
}
}
func handleQueryParam(w http.ResponseWriter, r *http.Request) {
// Get a specific query parameter by name
paramValue := chi.URLParam(r, "paramName")
// Alternatively, you can use r.URL.Query() to get all query parameters as a map
queryParams := r.URL.Query()
// Print the specific query parameter value
fmt.Println("Query Parameter Value:", paramValue)
// Print all query parameters
fmt.Println("All Query Parameters:", queryParams)
// Send a response back to the client
fmt.Fprintf(w, "Query Parameter Value: %s\n", paramValue)
fmt.Fprintf(w, "All Query Parameters: %+v\n", queryParams)
if queryParams[] == "hello" {
w.Write([]byte("World!"))
}
}
func main() {
r := chi.NewRouter()
// 1 ////////////////////////////////////////////////////////////
r.Use(middleware.Logger)
// r.Get("/", func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("Hello World!!!!"))
// })
// r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("World!"))
// })
// r.Get("/world", func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("Hello!"))
// })
// // 2 ////////////////////////////////////////////////////////////
//http.HandleFunc("/", handler)
//r.Get("/queryparam", checkURL)git
//http.HandleFunc("/", handler)
r.Get("/queryparam", handleQueryParam)
//
http.ListenAndServe(":3000", r)
// fmt.Println("Hello, World!!")
}