Conditional - If, Else if, Else

Part of Golang Mastery course

~15 min read
Interactive
Hands-on
Beginner-friendly

Conditional - If, Else if, Else

Here's an example of using if and else

example.go
package main
 
import (
string">"fmt"
)
 
func main() {
x := 42
 
if x == 40 {
fmt.Println(string">"Our value was 40")
} else {
fmt.Println(string">"Our value was not 40")
}
}
 

playground

We can also use else if as many times as we want within the if statement.

example.go
package main
 
import (
string">"fmt"
)
 
func main() {
x := 42
if x == 40 {
fmt.Println(string">"Our value was 40")
} else if x == 41 {
fmt.Println(string">"Our value was 41")
} else if x == 42 {
fmt.Println(string">"Our value was 42")
} else if x == 43 {
fmt.Println(string">"Our value was 43")
} else {
fmt.Println(string">"Our value was not 40")
}
}
 

playground

Your Progress

20 of 103 modules
19%
Started19% Complete
Previous
SpaceComplete
Next