Conditionals in Go are similar to conditionals in other languages.
The underlying type of any conditional operation is the bool
type, which can have the value of true
or false
.
Conditionals are often used as flow control mechanisms to check for various conditions.
For checking a particular case an if
statement can be used, which executes its code if the underlying condition is true
like this:
var value string
if value == "val" {
return "was val"
}
In scenarios involving more than one case many if
statements can be chained together using the else if
and else
statements.
var number int
result := "This number is "
if number > 0 {
result += "positive"
} else if number < 0 {
result += "negative"
} else {
result += "zero"
}
However, it is convention to avoid else
statements as Go promotes early returns:
func getVal(connected bool) int {
// The exceptional case should be in the `if` statemeent.
// In this case being `connected` is the default, `readLocalVal` the fallback.
if !connected {
// using an `early return` to remove the need for an `else` case
return readLocalVal()
}
return readVal()
}
If statements can also include a short initialization statement that can be used to initialize one or more variables for the if statement. For example:
num := 7
if v := 2 * num; v > 10 {
fmt.Println(v)
} else {
fmt.Println(num)
}
// Output: 14
Note: any variables created in the initialization statement go out of scope after the end of the if statement.
Coming from other languages one may be tempted to try to use one-line conditionals. Go does not support ternary operators or one-line conditionals. This is a purposeful design decision to limit the amount of possibilities, making code simpler and easier to read.
To learn more about this topic it is recommended to check this source: Go by Example: If/Else