-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsimple.go
More file actions
62 lines (55 loc) · 1.13 KB
/
simple.go
File metadata and controls
62 lines (55 loc) · 1.13 KB
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
package main
import (
"fmt"
"github.com/hetiansu5/urlquery"
)
// A SimpleChild is test structure
type SimpleChild struct {
Status bool `query:"status"`
Name string
}
// A SimpleData is test structure
type SimpleData struct {
Id int
Name string `query:"name"`
Child SimpleChild
Params map[string]int8 `query:"p"`
SliceChild []SimpleChild `query:"s"`
Password string `query:"-"` //- means ignoring password
Array [3]uint16
}
func main() {
data := SimpleData{
Id: 2,
Name: "http://localhost/test.php?id=2",
Child: SimpleChild{
Status: true,
},
Params: map[string]int8{
"one": 1,
"two": 2,
},
SliceChild: []SimpleChild{
{Status: true},
{Name: "honey"},
},
Password: "abc",
Array: [3]uint16{2, 3, 300},
}
fmt.Println(data)
//Marshal: from go structure to url query string
bytes, err := urlquery.Marshal(data)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(bytes))
//Unmarshal: from url query string to go structure
v := &SimpleData{}
err = urlquery.Unmarshal(bytes, v)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(*v)
}