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.
Cosa imparerai
- 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.
Punti chiave
- A runnable program lives in package main.
- It starts in func main.
fmt.Printlnprints a line of text.
Esempio Go eseguibile
main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
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.
I tuoi progressi sono salvati in questo browser Accedi per conservarli su ogni dispositivo.
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.
- Dispari, Pari o Nessuno dei due Classifica un numero in base al fatto che sia un intero dispari, un intero pari o nessuno dei due. Facile
- Eleva un Numero al Quadrato Moltiplica un intero per sé stesso. Facile