Files
curso-goland/intermedio.go
Manuel e13bee6d21 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.
2026-02-17 12:34:00 +01:00

106 lines
1.6 KiB
Go

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")
for i := 0; i < 5; i++ {
if i == 3 {
fmt.Println("Hasta el 3")
break
}
fmt.Println(i)
}
x := 2
switch x {
case 1:
fmt.Println("1")
case 2:
fmt.Println("2")
break
case 3:
fmt.Println("3")
default:
fmt.Println("default")
}
for i := 0; i < 5; i++ {
if i%2 == 0 {
fmt.Println(i, "es par")
continue
}
fmt.Println(i)
}
for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
if j == 2 {
continue // solo afecta al bucle interno
}
// 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())
}