Gaaker Go

Golang If else statement

The if statement can use else to add another branch. If the condition is false, it is executed, and the format is as follows.

if condition {
	// do something	
} else {
	// do something	
}

The following example uses the if else statement to check whether the string is empty.

The following is a complete code example.

package main

import "fmt"

func main() {
    var str string
    if len(str) == 0 { 
        fmt.Println("The string is empty")
    } else {
        fmt.Println("The string is not empty")
    }
}

Below is the program output:

The string is empty
< If else statement If else statement >