Fundamentos de Go
Hello World in Go: Structure of a Program
Write your first Go program and see what each part does: package main, import and func main.
Lo que aprenderás
- package main and func main
- Importing fmt
- Printing with fmt.Println
From source to output
- 1
main.goYou write the code - 2
go runGo compiles and starts it - 3
main()The program starts in main - 4
Hello, Go!The text appears
How a Go program runs
Every Go file starts with a package name. A program that you can run lives in package main and starts in the function main. import brings in other packages, such as fmt for printing.
fmt.Println prints its arguments and ends the line. Names that start with a capital letter, such as Println, are the ones a package makes available to other packages.
Puntos clave
- A runnable program lives in package main.
- It starts in func main.
fmt.Printlnprints a line of text.
Ejemplo ejecutable de Go
main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Hello, Go!Practica
Abre el ejemplo en el compilador online, cambia un valor o añade una línea y predice la salida antes de ejecutarlo. Después rompe el código a propósito y lee el mensaje del compilador.
¿Terminaste esta lección?
Márcala como aprendida para seguir tu progreso.
Tu progreso está guardado en este navegador Inicia sesión para conservarlo en todos tus dispositivos.
Practica con un reto
Usa lo que cubre esta lección para resolver un problema real. Cada reto se ejecuta en el navegador, con pruebas al instante y una revisión de IA.
- Impar, Par o Ninguno Clasifica un número según si es un entero impar, un entero par o ninguno de los dos. Fácil
- Elevar un número al cuadrado Multiplica un número entero por sí mismo. Fácil