Foundations
If Statements
Use if and else to choose work based on a condition.
If Statements
if_intro.go
package main
import "fmt"
func main() {
var temperature =
status := ""
if temperature >= 80 {
status = "warm"
} else {
status = "comfortable"
}
fmt.Println("temperature=", temperature)
fmt.Println("status=", status)
}
package main
import "fmt"
func main() {
var temperature =
status := ""
if temperature >= 80 {
status = "warm"
} else {
status = "comfortable"
}
fmt.Println("temperature=", temperature)
fmt.Println("status=", status)
}
package main
import "fmt"
func main() {
var temperature =
status := ""
if temperature >= 80 {
status = "warm"
} else {
status = "comfortable"
}
fmt.Println("temperature=", temperature)
fmt.Println("status=", status)
}
if statement
An `if` statement runs one block when its condition is true and an `else` block when it is false.