create an if statement with an else in go

To create an if statement with an else clause in Go, you can use the if-else syntax. This is a control structure that allows you to perform different actions based on whether a condition is true or false.

Here's an example:

main.go
package main

import "fmt"

func main() {

    age := 21

    if age >= 18 {
        fmt.Println("You are an adult")
    } else {
        fmt.Println("You are a minor")
    }
}
177 chars
15 lines

In this example, we define a variable age with a value of 21. We then use an if statement to check if the age is greater than or equal to 18. If the condition is true, we print "You are an adult". If the condition is false, we print "You are a minor". The else clause is optional and is executed when the condition in the if statement is false.

gistlibby LogSnag