-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
90 lines (77 loc) · 1.39 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
def "github.com/rebirthlee/golang-default"
)
type InitString string
type Sample struct {
ExportField string `def:"export field"`
notExportField string
StringField *InitString
}
func (i *InitString) Init() {
fmt.Printf("InitString(%p) call Init\n", i)
*i = "Hello String Field"
}
func (s *Sample) Init() {
fmt.Printf("Sample(%p) call Init\n", s)
s.notExportField = "not export field"
}
func main() {
{
// New
i, err := def.New(Sample{})
if err == nil {
s := i.(*Sample)
showFields(s)
}
}
{
// MustNew
s := def.MustNew(Sample{}).(*Sample)
showFields(s)
}
{
// JustNew
i, err := def.JustNew(Sample{})
if err == nil {
s := i.(*Sample)
showFields(s)
}
}
{
// Init
s := Sample{}
if err := def.Init(&s); err != nil {
// ...err
fmt.Println("Init, Handle Error")
} else {
showFields(&s)
}
}
{
// MustInit
s := Sample{}
def.MustInit(&s)
showFields(&s)
}
{
// JustInit
s := Sample{}
if err := def.JustInit(&s); err != nil {
// ...err
fmt.Println("JustInit, Handle Error")
} else {
showFields(&s)
}
}
}
func showFields(s *Sample) {
fmt.Printf("Struct Address : %p\n", s)
fmt.Println("p.ExportField :", s.ExportField)
fmt.Println("p.notExportField :", s.notExportField)
fmt.Printf("p.StringField : %p\n", s.StringField)
fmt.Println("*p.StringField :", *s.StringField)
println()
println()
}