File tree Expand file tree Collapse file tree 1 file changed +111
-0
lines changed Expand file tree Collapse file tree 1 file changed +111
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ )
6
+
7
+ type person struct {
8
+ fname string
9
+ lname string
10
+ ficf []string
11
+ }
12
+
13
+ func main () {
14
+
15
+ fmt .Println ("\n Solution 2\n " )
16
+ solution2 ()
17
+
18
+ fmt .Println ("\n Solution 3\n " )
19
+ solution3 ()
20
+
21
+ fmt .Println ("\n Solution 4\n " )
22
+ solution4 ()
23
+ }
24
+
25
+ func solution2 () {
26
+
27
+ //Creation of maps using structs
28
+ p1 := person {
29
+ fname : "aj" ,
30
+ lname : "kul" ,
31
+ ficf : []string {"kasata" , "vanilla" },
32
+ }
33
+ p2 := person {
34
+ fname : "Ro" ,
35
+ lname : "kul1" ,
36
+ ficf : []string {"rainbow" , "mint" , "chocolate" },
37
+ }
38
+
39
+ /*
40
+ fmt.Println(p1)
41
+ for _, icecream := range p1.ficf {
42
+ fmt.Println(icecream)
43
+ }
44
+ for _, icecream := range p2.ficf {
45
+ fmt.Println(icecream)
46
+ }
47
+ */
48
+
49
+ m := map [string ]person {
50
+ p1 .lname : p1 ,
51
+ p2 .lname : p2 ,
52
+ }
53
+
54
+ fmt .Println (m ["kul" ])
55
+ }
56
+
57
+
58
+ type vehicle struct {
59
+ doors int
60
+ color string
61
+ }
62
+
63
+ type truck struct {
64
+ vehicle
65
+ fourWheel bool
66
+ }
67
+
68
+ type sedan struct {
69
+ vehicle
70
+ luxury bool
71
+ }
72
+
73
+ func solution3 () {
74
+
75
+ // Usage of embedded structs
76
+ t1 := truck {
77
+ vehicle : vehicle {
78
+ doors : 2 ,
79
+ color : "red" },
80
+ fourWheel : true ,
81
+ }
82
+
83
+ s1 := sedan {
84
+ vehicle : vehicle {
85
+ doors : 4 ,
86
+ color : "black" ,
87
+ },
88
+ luxury : false ,
89
+ }
90
+
91
+ fmt .Println (t1 )
92
+ fmt .Println (s1 )
93
+ fmt .Printf ("%d %s %t" , t1 .doors , t1 .color , t1 .fourWheel )
94
+
95
+
96
+ }
97
+
98
+
99
+
100
+ func solution4 () {
101
+
102
+ // Usage of anonymous struct
103
+ t1 := struct {
104
+ doors int
105
+ color string
106
+ }{
107
+ doors : 4 ,
108
+ color : "red" ,
109
+ }
110
+ fmt .Println (t1 )
111
+ }
You can’t perform that action at this time.
0 commit comments