-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructs.go
87 lines (76 loc) · 1.17 KB
/
structs.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
package main
import "fmt"
type user struct {
name string
phone int
}
// nested struct
type msg struct {
message string
sender user
recipient user
}
type car struct {
make string
model string
// anonymous struct
Wheel struct {
radius int
material string
}
}
// embedded struct
type ownedCar struct {
user
make string
model string
}
type cuboid struct {
length int
width int
height int
}
// take struct as self
func (c cuboid) volume() int {
return c.length * c.width * c.height
}
func main() {
m := user {
phone: 148255510961,
name: "Sumit",
}
fmt.Println(m)
m2 := msg {}
m2.message = "Thanks for signing up"
m2.sender.phone = 1234567890
m2.sender.name = "Boot dev"
m2.recipient.phone = 148255510961
m2.recipient.name = "Sumit"
fmt.Println(m2)
// anonymous structs
myCar := struct {
make string
model string
} {
make: "tesla",
model: "model 3",
}
fmt.Println(myCar)
// embedded structs
myCar2 := ownedCar {
make: "bugatti",
model: "chiron",
user: user {
name: "Sumit",
phone: 148255510961,
},
}
fmt.Println(myCar2)
// struct methods
c := cuboid {
length: 7,
width: 5,
height: 16,
}
fmt.Println(c.volume())
}