-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
46 lines (33 loc) · 836 Bytes
/
example_test.go
File metadata and controls
46 lines (33 loc) · 836 Bytes
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
package querydecoder_test
import (
"log"
"net/http"
"github.com/ritwickdey/querydecoder"
)
type User struct {
IsSuperUser bool `query:"is_super_user"`
UserName string `query:"user_name"`
UserID int64 `query:"user_id"`
}
func handler1(w http.ResponseWriter, r *http.Request) {
u1 := User{}
// Decode into struct
err := querydecoder.New(r.URL.Query()).Decode(&u1)
if err != nil {
panic(err)
}
log.Println(u1)
}
func handler2(w http.ResponseWriter, r *http.Request) {
var isDog bool
err := querydecoder.New(r.URL.Query()).DecodeField("is_dog", &isDog)
// if `is_dog` query param is not there, it'll not modify the value of the variable.
if err != nil {
panic(err)
}
}
func Example() {
http.HandleFunc("/foo1", handler1)
http.HandleFunc("/foo2", handler2)
http.ListenAndServe(":8090", nil)
}