/
struct3.go
107 lines (79 loc) · 2.02 KB
/
struct3.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Go in action
// @jeffotoni
// 2019-01-24
// different ways to inizialize a struct
package main
import "fmt"
type Address struct {
City string `json:"city"`
Neighborhood string `json:"neighborhood"`
Zipcode string `json:"zipcode"`
}
type ApiLogin struct {
Name string `json:"name"`
Cpf string `json:"cpf"`
Login string `json:"login"`
Email string `json:"email"`
// anonymous
And1 *struct {
City string
}
// pointer
And2 *Address
// list Address
And3 []Address
}
func main() {
// different ways to inizialize a struct
//
//
apilogin1 := &ApiLogin{Name: "@jeffotoni", Cpf: "093.393.334-34",
And1: &struct{ City string }{City: "BH"}}
fmt.Println(apilogin1)
fmt.Println(apilogin1.Name)
fmt.Println(apilogin1.And1)
fmt.Println(apilogin1.And1.City)
apilogin2 := &ApiLogin{Name: "@jeffotoni",
Cpf: "093.393.334-34",
And2: &Address{City: "BH"}}
fmt.Println(apilogin2)
fmt.Println(apilogin2.Name)
fmt.Println(apilogin2.And2)
fmt.Println(apilogin2.And2.City)
apilogin3 := &ApiLogin{Name: "@jeffotoni", Cpf: "093.393.334-34",
And1: &struct{ City string }{City: "BH"},
And2: &Address{City: "BH"}}
fmt.Println(apilogin3)
fmt.Println(apilogin3.Name)
fmt.Println(apilogin3.And1)
fmt.Println(apilogin3.And1.City)
fmt.Println(apilogin3.And2)
fmt.Println(apilogin3.And2.City)
var apilogin4 ApiLogin
fmt.Println(apilogin4)
apilogin5 := ApiLogin{}
fmt.Println(apilogin5)
apilogin6 := &ApiLogin{}
fmt.Println(apilogin6)
apilogin7 := new(ApiLogin)
fmt.Println(apilogin7)
// another way to feed the struct
g1add := Address{City: "Belo Horizonte"}
g2add := Address{City: "Curitiba"}
// declaring as list
gall := []Address{}
// add items
gall = append(gall, g1add)
gall = append(gall, g2add)
fmt.Println(gall)
// initializes Struct
apil3 := ApiLogin{}
// recive same type
apil3.And3 = gall
// show struct
fmt.Println(apil3)
// another way to initialize and feed the struct list
apil3.And3 = []Address{{City: "Sao Paulo"}, {City: "Brasilia"}}
// show struct
fmt.Println(apil3)
}