Go Comparison Operators 🎯

beginner
9 min

Go Comparison Operators 🎯

Welcome to a fun and engaging lesson on Go comparison operators! This tutorial will guide you through the essentials of comparing values in Go, a powerful programming language. By the end, you'll be able to confidently use comparison operators in your own projects.

What are Comparison Operators? 📝

In Go, comparison operators are used to compare the values of variables or expressions. They help determine relationships between values, like equality, inequality, or ordering. Let's dive into the comparison operators available in Go:

  1. Equal (==) Compares if two operands are equal.

  2. Not Equal (!=) Checks if two operands are not equal.

  3. Less Than (<) Determines if the left operand is less than the right operand.

  4. Greater Than (>) Checks if the left operand is greater than the right operand.

  5. Less Than or Equal (<=) Verifies if the left operand is less than or equal to the right operand.

  6. Greater Than or Equal (>=) Confirms if the left operand is greater than or equal to the right operand.

Practical Examples 💡

Let's put these operators into practice with some examples:

go
package main import "fmt" func main() { x := 10 y := 20 // Equal if x == y { fmt.Println("x is equal to y") } else { fmt.Println("x is not equal to y") } // Not Equal if x != y { fmt.Println("x is not equal to y") } else { fmt.Println("x is equal to y") } // Less Than if x < y { fmt.Println("x is less than y") } else { fmt.Println("x is not less than y") } // Greater Than if x > y { fmt.Println("x is greater than y") } else { fmt.Println("x is not greater than y") } // Less Than or Equal if x <= y { fmt.Println("x is less than or equal to y") } else { fmt.Println("x is not less than or equal to y") } // Greater Than or Equal if x >= y { fmt.Println("x is greater than or equal to y") } else { fmt.Println("x is not greater than or equal to y") } }

When you run this code, it will output:

bash
x is not equal to y x is not equal to y x is not less than y x is not greater than y x is less than or equal to y x is not greater than or equal to y

Quiz Time! 🎮

Quick Quiz
Question 1 of 1

What is the output of the following code?

Happy coding! 🚀