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.
This commit is contained in:
dietpi committed 2026-02-17 12:34:00 +01:00
1 parent 24102d50a7
commit e13bee6d21
2 files changed
+65 -3

No files matched your search

+3
View File
@@ -9,3 +9,6 @@ https://www.youtube.com/watch?v=ID9NZ88JeOE
//intermedio.go //intermedio.go
1. (00:38:46) continue 1. (00:38:46) continue
2. (00:56:07) struct
//avanzado.go
+62 -3
View File
@@ -2,6 +2,25 @@ package main
import "fmt" 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() { func main() {
fmt.Println("Hola mundo") fmt.Println("Hola mundo")
@@ -28,7 +47,7 @@ func main() {
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
if i%2 == 0 { if i%2 == 0 {
fmt.Println(i, " es par") fmt.Println(i, "es par")
continue continue
} }
fmt.Println(i) fmt.Println(i)
@@ -37,10 +56,50 @@ func main() {
for i := 1; i <= 3; i++ { for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ { for j := 1; j <= 3; j++ {
if j == 2 { 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())
} }