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.

  • Principiante
  • 2 min de lectura
  • 1 ejemplo ejecutable
Lo que aprenderás
  • package main and func main
  • Importing fmt
  • Printing with fmt.Println
From source to output
  1. 1main.goYou write the code
  2. 2go runGo compiles and starts it
  3. 3main()The program starts in main
  4. 4Hello, 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

  1. A runnable program lives in package main.
  2. It starts in func main.
  3. fmt.Println prints a line of text.

Ejemplo ejecutable de Go

Go
Ejecutar código →
main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello, Go!")
}
Obtén un botón Run para este ejemplo Resultado de ejecutar el código
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.

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.

Todos los retos