Go programming Language if else

Go Programming Language if else

if else is one of the approach to build the decision in go programming.

Syntax:
If condition {
//task
} else {
// another task
}

here condition can be obtained by logical or comparison operations.

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
                                                                           	 
	// declare variables                                                   	 
	var a, b int                                                           	 
                                                                           	 
	// assign variables                                                    	 
	a = 10                                                                 	 
	b = 6                                                                  	 
                                                                           	 
	fmt.Println("Compares a and b variables")                              	 
	if a>b {                                                               	 
    	fmt.Printf("%d is greater than or equal to %d!\n", a, b)           	 
	} else{                                                                	 
    	fmt.Printf("%d is less than %d!\n", a, b)                          	 
	}                                                                      	 
}
Output:
$ go build if-else.go
$ ./if-else
Compares a and b variables
10 is greater than or equal to 6!

Starting else block in new line raises build error in Go programming

Example:
package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
                                                                           	 
	// declare variables                                                   	 
	var a, b int                                                           	 
                                                                           	 
	// assign variables                                                    	 
	a = 10                                                                 	 
	b = 6                                                                  	 
                                                                           	 
	fmt.Println("Compares a and b variables")                              	 
	if a>b {                                                               	 
    	fmt.Printf("%d is greater than or equal to %d!\n", a, b)           	 
	}                                                                      	 
	else{                                                                  	 
    	fmt.Printf("%d is less than %d!\n", a, b)                          	 
	}                                                                      	 
}
Build error
$ go build if-else.go
# command-line-arguments
./if-else.go:18: syntax error: unexpected else, expecting }

Go programming else if

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
                                                                           	 
	// declare variables                                                   	 
	var a, b int                                                           	 
                                                                           	 
	// assign variables                                                    	 
	a = 10                                                                 	 
	b = 6                                                                  	 
                                                                           	 
	fmt.Println("Compares a and b variables")                              	 
	if a==b {                                                              	 
    	fmt.Printf("%d is equal to %d!\n", a, b)                           	 
	} else if a>b{                                                         	 
    	fmt.Printf("%d is greater than %d!\n", a, b)                       	 
	} else {                                                               	 
    	fmt.Printf("%d is less than %d!\n", a, b)                          	 
	}                                                                      	 
} 
Output:
$ go build if-elseif.go
$ ./if-elseif
Compares a and b variables
10 is greater than 6!