Skip to content

Commit 5b465de

Browse files
Ejercises Struct
1 parent 46158bd commit 5b465de

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed

ejercicio5-2.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type persona struct {
6+
nombre string
7+
apellido string
8+
saboresFav []string
9+
}
10+
11+
func main() {
12+
13+
p1 := persona{
14+
nombre: "Diego",
15+
apellido: "Lopez",
16+
saboresFav: []string{"Chantillí", "Vainilla", "Milkey Way"}, //ülitmo elemento lleva ,
17+
}
18+
19+
p2 := persona{
20+
nombre: "Sergio",
21+
apellido: "Algarin",
22+
saboresFav: []string{"Brownie", "Ron con pasas", "Oreo"},
23+
}
24+
25+
m := map[string]persona{
26+
p1.apellido: p1,
27+
p2.apellido: p2,
28+
}
29+
30+
for k, v := range m {
31+
fmt.Println("\t", k, v)
32+
for i, v := range v.saboresFav {
33+
fmt.Println("\t", i, v)
34+
}
35+
}
36+
fmt.Println("=============================")
37+
38+
for _, v := range m {
39+
fmt.Println("\t", v)
40+
}
41+
42+
}

ejercicio5-3.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type vehiculo struct {
6+
puerta int
7+
color string
8+
}
9+
type camion struct {
10+
vehiculo
11+
cuatroRuedas bool
12+
}
13+
type sedan struct {
14+
vehiculo
15+
lujos bool
16+
}
17+
18+
func main() {
19+
20+
Camion := camion{
21+
vehiculo: vehiculo{
22+
puerta: 2,
23+
color: "Amarillo",
24+
},
25+
cuatroRuedas: false,
26+
}
27+
28+
Camion2 := sedan{
29+
vehiculo: vehiculo{
30+
puerta: 3,
31+
color: "Fuxcisa",
32+
},
33+
lujos: true,
34+
}
35+
36+
fmt.Println("", Camion)
37+
fmt.Println("", Camion2)
38+
39+
}

ejercicio5-4.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
s := struct {
7+
nombre string
8+
amigos map[string]int
9+
bebidasFav []string
10+
}{
11+
nombre: "Dirego",
12+
amigos: map[string]int{
13+
"Lopez": 23232,
14+
"SErgio": 456456,
15+
"Amador": 9899,
16+
},
17+
bebidasFav: []string{
18+
"agua",
19+
"Naranjada",
20+
"Cervezas",
21+
},
22+
}
23+
24+
fmt.Println(s.nombre)
25+
fmt.Println("\t Amigos:")
26+
for k, v := range s.amigos {
27+
fmt.Println("\t\t", k, v)
28+
}
29+
fmt.Println("\t Bebidas:")
30+
for i, v := range s.bebidasFav {
31+
fmt.Println("\t\t", i, v)
32+
}
33+
}

structs.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type persona struct {
6+
nombre string
7+
apellido string
8+
edad int
9+
}
10+
11+
func main() {
12+
13+
// Creating a new instance of the struct persona and assigning it to the variable pl.
14+
pl := persona{
15+
nombre: "Diego Alberto",
16+
apellido: "Lopez Murillo",
17+
edad: 25,
18+
}
19+
p2 := persona{
20+
nombre: "Alejandro",
21+
apellido: "Martinez",
22+
edad: 18,
23+
}
24+
25+
fmt.Println(pl)
26+
fmt.Println(pl.edad)
27+
fmt.Println(p2.apellido)
28+
fmt.Printf("%T\n", pl)
29+
}

structsEmebebed.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type persona struct {
6+
nombre string
7+
apellido string
8+
edad int
9+
}
10+
11+
type agenteSecreto struct {
12+
persona
13+
lpm bool
14+
}
15+
16+
func main() {
17+
18+
// Creating a new instance of the struct persona and assigning it to the variable pl.
19+
pl := persona{
20+
nombre: "Diego Alberto",
21+
apellido: "Lopez Murillo",
22+
edad: 25,
23+
}
24+
p2 := persona{
25+
nombre: "Alejandro",
26+
apellido: "Martinez",
27+
edad: 18,
28+
}
29+
30+
p3 := agenteSecreto{
31+
persona: p2,
32+
lpm: true,
33+
}
34+
35+
fmt.Println(pl)
36+
fmt.Println(pl.edad)
37+
fmt.Println(p2.apellido)
38+
fmt.Println(p3.persona, p3.nombre)
39+
fmt.Printf("%T\n", pl)
40+
}

0 commit comments

Comments
 (0)