Go Language Comparison Operators

Go Language Comparison Operators

Comparison operators are used to determine equality or difference among variables or values.

Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Go Program Example for Comparison Operators

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
                                                                           	 
	// declare variables                                                   	 
	var a, b int                                                           	 
                                                                           	 
	a = 10                                                                 	 
	b = 5                                                                  	 
                                                                           	 
	// different comparison operations                                     	 
	fmt.Printf("%d is greater than %d: %t\n",a, b, a>b)                    	 
	fmt.Printf("%d is greater than or equal to %d: %t\n",a, b, a>=b)       	 
	fmt.Printf("%d is less than %d: %t\n",a, b, a<b)                       	 
	fmt.Printf("%d is less than or equal to %d: %t\n",a, b, a<=b)          	 
	fmt.Printf("%d is equal to %d: %t\n",a, b, a==b)                       	 
	fmt.Printf("%d is not equal to %d: %t\n",a, b, a!=b)                   	 
} 
Output:
$ go build comparison-operators.go
$ ./comparison-operators
10 is greater than 5: true
10 is greater than or equal to 5: true
10 is less than 5: false
10 is less than or equal to 5: false
10 is equal to 5: false
10 is not equal to 5: true