create an if statement with an else if and else in go

To create an if statement with an else if and else in Go, you can use the following syntax:

main.go
if condition1 {
    // code to be executed if condition1 is true
} else if condition2 {
    // code to be executed if condition1 is false and condition2 is true
} else {
    // code to be executed if both condition1 and condition2 are false
}
243 chars
8 lines

Here's an example:

main.go
package main

import "fmt"

func main() {
    age := 20

    if age < 18 {
        fmt.Println("You are not eligible to vote.")
    } else if age >= 18 && age < 60 {
        fmt.Println("You are eligible to vote.")
    } else {
        fmt.Println("You are not eligible to vote.")
    }
}
289 chars
16 lines

In this example, the first condition checks if age is less than 18. If it's true, the program prints "You are not eligible to vote." If it's false, the program checks the second condition. If that is true, the program prints "You are eligible to vote." If both conditions are false, the program executes the code in the else block and prints "You are not eligible to vote."

related categories

gistlibby LogSnag