Bases 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.
Ce que vous allez apprendre
- 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.
Points clés
- A runnable program lives in package main.
- It starts in func main.
fmt.Printlnprints a line of text.
Exemple Go exécutable
main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Hello, Go!S'entraîner
Ouvrez l'exemple dans le compilateur en ligne, modifiez une valeur ou ajoutez une ligne, et devinez la sortie avant de l'exécuter. Cassez ensuite le code exprès et lisez le message du compilateur.
Leçon terminée ?
Marquez-la comme apprise pour suivre votre progression.
Votre progression est enregistrée dans ce navigateur Connectez-vous pour la garder sur tous vos appareils.
S'entraîner avec un défi
Utilisez le contenu de cette leçon pour résoudre un vrai problème. Chaque défi s'exécute dans le navigateur, avec des tests instantanés et une revue par l'IA.
- Impair, Pair ou Aucun des deux Classifiez un nombre selon qu'il s'agit d'un entier impair, d'un entier pair, ou d'aucun des deux. Facile
- Élever un nombre au carré Multiplier un entier par lui-même. Facile