> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Comparison Operators

> Explains Go comparison operators, their meanings, type comparability rules, and examples for equality and ordering operations.

Comparison operators compare two operands and return a Boolean value: true or false. Equality operators (==, !=) require operands to be of comparable types (typically the same type or types that the language allows to be compared). Ordering operators (\<, \<=, >, >=) require ordered types such as numeric types and strings. Common comparisons include checking whether two strings match, if two numbers are equal, or if one number is greater than another.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AGJumo2kLOdCQw6n/images/Golang/Operators-and-Control-Flow/Comparison-Operators/comparison-operators-same-type-examples.jpg?fit=max&auto=format&n=AGJumo2kLOdCQw6n&q=85&s=e2fb434a26863912660e659bad4eec30" alt="A presentation slide titled &#x22;Comparison Operators&#x22; with bullet points explaining that they compare two operands to yield a Boolean, require same data types, and giving examples (string match, number equality, greater‑than). The slide has a dark background and a small KodeKloud logo in the top-right." width="1920" height="1080" data-path="images/Golang/Operators-and-Control-Flow/Comparison-Operators/comparison-operators-same-type-examples.jpg" />
</Frame>

<Callout icon="warning" color="#FF6B6B">
  Not all types are comparable using == and !=. Types such as slices, maps, and functions cannot be compared with equality operators — attempting to do so results in a compile-time error. Structs and arrays are comparable only if all their fields/elements are comparable.
</Callout>

We have the following comparison operators: equal (==), not equal (!=), less than (\<), less than or equal to (\<=), greater than (>), and greater than or equal to (>=).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AGJumo2kLOdCQw6n/images/Golang/Operators-and-Control-Flow/Comparison-Operators/comparison-operators-equality-inequality.jpg?fit=max&auto=format&n=AGJumo2kLOdCQw6n&q=85&s=c069b7269d81f51cdc418b2e73a3f1a7" alt="A dark-themed slide titled &#x22;Comparison Operators&#x22; showing six boxed symbols: ==, !=, <, <=, >, >=. Each symbol is labeled beneath as equal, not equal, less than, less than or equal to, greater than, and greater than or equal to." data-og-width="1920" width="1920" data-og-height="1080" height="1080" data-path="images/Golang/Operators-and-Control-Flow/Comparison-Operators/comparison-operators-equality-inequality.jpg" data-optimize="true" data-opv="3" srcset="https://mintcdn.com/kodekloud-c4ac6d9a/AGJumo2kLOdCQw6n/images/Golang/Operators-and-Control-Flow/Comparison-Operators/comparison-operators-equality-inequality.jpg?w=280&fit=max&auto=format&n=AGJumo2kLOdCQw6n&q=85&s=d2280c3e41c46232ab39c5f253afafd9 280w, https://mintcdn.com/kodekloud-c4ac6d9a/AGJumo2kLOdCQw6n/images/Golang/Operators-and-Control-Flow/Comparison-Operators/comparison-operators-equality-inequality.jpg?w=560&fit=max&auto=format&n=AGJumo2kLOdCQw6n&q=85&s=bea0581882efc4ddba61f9f44453f68d 560w, https://mintcdn.com/kodekloud-c4ac6d9a/AGJumo2kLOdCQw6n/images/Golang/Operators-and-Control-Flow/Comparison-Operators/comparison-operators-equality-inequality.jpg?w=840&fit=max&auto=format&n=AGJumo2kLOdCQw6n&q=85&s=c6028d1296fdf60205b7c5ceae722a1b 840w, https://mintcdn.com/kodekloud-c4ac6d9a/AGJumo2kLOdCQw6n/images/Golang/Operators-and-Control-Flow/Comparison-Operators/comparison-operators-equality-inequality.jpg?w=1100&fit=max&auto=format&n=AGJumo2kLOdCQw6n&q=85&s=89c1122312e2e600a22cc346232a8b1c 1100w, https://mintcdn.com/kodekloud-c4ac6d9a/AGJumo2kLOdCQw6n/images/Golang/Operators-and-Control-Flow/Comparison-Operators/comparison-operators-equality-inequality.jpg?w=1650&fit=max&auto=format&n=AGJumo2kLOdCQw6n&q=85&s=5dafe7789574c49ecde16ea44f897246 1650w, https://mintcdn.com/kodekloud-c4ac6d9a/AGJumo2kLOdCQw6n/images/Golang/Operators-and-Control-Flow/Comparison-Operators/comparison-operators-equality-inequality.jpg?w=2500&fit=max&auto=format&n=AGJumo2kLOdCQw6n&q=85&s=70a49d29eeda3dd2b3eeaea49bcdbbc8 2500w" />
</Frame>

<Callout icon="lightbulb" color="#1CB2FE">
  When comparing values in Go, make sure both operands are of comparable types. Use explicit conversions when necessary (for example, converting between int types) and prefer clear, readable comparisons to avoid subtle bugs.
</Callout>

Operator reference

| Operator | Meaning                  | Typical Use Case                     |
| -------- | ------------------------ | ------------------------------------ |
| ==       | equal                    | Compare identical values or strings  |
| !=       | not equal                | Check inequality                     |
| \<       | less than                | Order comparisons (numbers, strings) |
| \<=      | less than or equal to    | Order or boundary checks             |
| >        | greater than             | Order comparisons                    |
| >=       | greater than or equal to | Order or boundary checks             |

Examples

Equal (==)
The equal operator returns true when the two values are equal.

```go theme={null}
package main

import "fmt"

func main() {
	var city string = "Kolkata"
	var city2 string = "Calcutta"
	fmt.Println(city == city2)
}
```

```bash theme={null}
$ go run main.go
false
```

Not equal (!=)
The not-equal operator returns true when the two values are not equal.

```go theme={null}
package main

import "fmt"

func main() {
	var city string = "Kolkata"
	var city2 string = "Calcutta"
	fmt.Println(city != city2)
}
```

```bash theme={null}
$ go run main.go
true
```

Less than (\<)
The less-than operator returns true when the left operand is strictly less than the right operand.

```go theme={null}
package main

import "fmt"

func main() {
	var a, b int = 5, 10
	fmt.Println(a < b)
}
```

```bash theme={null}
$ go run main.go
true
```

Less than or equal to (\<=)
This operator returns true when the left operand is less than or equal to the right operand.

```go theme={null}
package main

import "fmt"

func main() {
	var a, b int = 10, 10
	fmt.Println(a <= b)
}
```

```bash theme={null}
$ go run main.go
true
```

Greater than (>)
The greater-than operator returns true when the left operand is strictly greater than the right operand.

```go theme={null}
package main

import "fmt"

func main() {
	var a, b int = 20, 10
	fmt.Println(a > b)
}
```

```bash theme={null}
$ go run main.go
true
```

Greater than or equal to (>=)
This operator returns true when the left operand is greater than or equal to the right operand.

```go theme={null}
package main

import "fmt"

func main() {
	var a, b int = 20, 20
	fmt.Println(a >= b)
}
```

```bash theme={null}
$ go run main.go
true
```

Links and References

* Go Language Specification — Expressions: [https://golang.org/ref/spec#Comparison\_operators](https://golang.org/ref/spec#Comparison_operators)
* Go Documentation: [https://golang.org/doc/](https://golang.org/doc/)
* Effective Go: [https://golang.org/doc/effective\_go.html](https://golang.org/doc/effective_go.html)

That's it for this lesson.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/golang/module/036484d6-1e16-46e2-908d-77e545bde2bf/lesson/ea9cf2b6-be7e-4a39-a712-cba0525c0553" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/golang/module/036484d6-1e16-46e2-908d-77e545bde2bf/lesson/501bf696-1e86-4055-993b-bcf4654f1805" />
</CardGroup>
