Comparison Operators in Go #
Comparison operators are used to compare two values.
Note: The return value of a comparison is either
true
orfalse
.
Example: Greater Than #
package main
import ("fmt")
func main() {
var x = 5
var y = 3
fmt.Println(x > y) // true because 5 is greater than 3
}
List of Comparison Operators #
Operator | Name | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
You will learn more about comparison operators and how to use them in the Go Conditions chapter.