Fondamenti di 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 di lettura
  • 1 esempio eseguibile
Cosa imparerai
  • 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.

Punti chiave

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

Esempio Go eseguibile

Go
Esegui il codice →
main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello, Go!")
}
Crea un pulsante Run per questo esempio Risultato dell'esecuzione
Hello, Go!

Esercitati

Apri l'esempio nel compilatore online, cambia un valore o aggiungi una riga e prova a prevedere l'output prima di eseguirlo. Poi rompi il codice di proposito e leggi il messaggio del compilatore.

Hai finito questa lezione?

Segnala come imparata per tenere traccia dei tuoi progressi.

Esercitati con una sfida

Usa ciò che questa lezione spiega per risolvere un problema reale. Ogni sfida gira nel browser, con test immediati e una revisione dell'IA.

Tutte le sfide