From e13bee6d21377e0b891a2c548de23baad98ce3e5 Mon Sep 17 00:00:00 2001 From: Manuel Date: Tue, 17 Feb 2026 12:34:00 +0100 Subject: [PATCH] Add struct and method examples Includes new struct definitions and methods for Celsius, Persona, and Rectangulo. Also adds examples of their usage in the main function. --- README.md | 3 +++ intermedio.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8f9985b..c3e5268 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,6 @@ https://www.youtube.com/watch?v=ID9NZ88JeOE //intermedio.go 1. (00:38:46) continue +2. (00:56:07) struct + +//avanzado.go diff --git a/intermedio.go b/intermedio.go index 8b0d268..053acca 100644 --- a/intermedio.go +++ b/intermedio.go @@ -2,6 +2,25 @@ package main import "fmt" +type Celsius float64 + +func (c Celsius) toFahrenheit() float64 { + return float64(c * 9 / 5 * 32) +} + +type Persona struct { + Nombre string + Edad int +} + +type Rectangulo struct { + Width, Height float64 +} + +func (r Rectangulo) Area() float64 { + return r.Width * r.Height +} + func main() { fmt.Println("Hola mundo") @@ -28,7 +47,7 @@ func main() { for i := 0; i < 5; i++ { if i%2 == 0 { - fmt.Println(i, " es par") + fmt.Println(i, "es par") continue } fmt.Println(i) @@ -37,10 +56,50 @@ func main() { for i := 1; i <= 3; i++ { for j := 1; j <= 3; j++ { if j == 2 { - continue + continue // solo afecta al bucle interno } - fmt.Println("i=", i, "j=", j) + // fmt.Println("i=", i, "j=", j) + fmt.Printf("i=%d j=%d\n", i, j) } } + nums := []int{1, 2, 3, 4, 5} + for i, num := range nums { + fmt.Printf("index=%d valor=%d\n", i, num) + } + + m := map[string]int{"uno": 1, "dos": 2, "tres": 3} + for k, v := range m { + fmt.Printf("key=%s valor=%d\n", k, v) + } + + x = 2 + switch x { + case 1: + fmt.Println("1") + case 2: + fmt.Println("2") + fallthrough + case 3: + fmt.Println("3") + default: + fmt.Println("default") + } + + // defer fmt.Println("salida") // deferido al final de la ejecución + fmt.Println("salida2") + + for i := 0; i < 5; i++ { + // defer fmt.Println(i) // deferido al final de la ejecución + } + fmt.Println("Fin del loop") + + temp := Celsius(100) + fmt.Println(temp.toFahrenheit()) + + p := Persona{Nombre: "Juan", Edad: 30} + fmt.Println(p.Nombre) + + r := Rectangulo{Width: 10, Height: 20} + fmt.Println(r.Area()) }