5. If, else and switch in Go — A Book of Go

Louis Petrik
May 15, 2024
Source: the author

If and else

If-statements can have a short statement, e. g., for declaring the variable used:

if x := math.Pi; x < 3 {
fmt.Println("x is less than 3")
} else {
fmt.Println("x is greater than or equal to 3")
}

The variables used are only available in the scope of the if-statement.

Switch

--

--