Valeurs zéro
Contexte
Section intitulée « Contexte »En Go, les variables déclarées sans valeur initiale explicite reçoivent automatiquement une valeur zéro. C’est la valeur par défaut pour chaque type.
| Type | Valeur zéro |
|---|---|
bool | false |
numérique (int, float, etc.) | 0 |
string | "" (chaîne vide) |
| pointeurs, slices, maps, canaux, fonctions, interfaces | nil |
Déclarez des variables sans initialisation et affichez leurs valeurs zéro.
Code exemple
Section intitulée « Code exemple »package main
import "fmt"
func main() { var a bool var b int var c float64 var d string var e []int var f map[string]int
fmt.Printf("bool: %v\n", a) fmt.Printf("int: %v\n", b) fmt.Printf("float64: %v\n", c) fmt.Printf("string: %q\n", d) fmt.Printf("slice: %v\n", e) fmt.Printf("map: %v\n", f)}bool: falseint: 0float64: 0string: ""slice: []map: map[]