Go Basics: Operators and Expressions Explained with Examples

Author Profile Pic
Anurag
Published on Sat Jul 08 2023 ~ 4 min read
Go Basics: Operators and Expressions Explained with Examples

In Go programming, understanding operators and expressions is fundamental to writing efficient and effective code. Operators are symbols that perform operations on operands, such as variables, constants, or values. Expressions, on the other hand, are combinations of operators, operands, and other expressions that evaluate to a value. In this blog post, we will explore the various operators available in Go and learn how to work with expressions using practical examples.


Arithmetic Operators:


Go provides a set of arithmetic operators for performing mathematical operations. Here are the commonly used arithmetic operators:

  • Addition (+): Adds two operands together.
  • Subtraction (-): Subtracts the second operand from the first.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the first operand by the second.
  • Modulus (%): Returns the remainder of the division operation.
  • Increment (++): Increases the value of an operand by 1.
  • Decrement (--): Decreases the value of an operand by 1.


Let's see these operators in action with some examples:


package main


import "fmt"


func main() {
    var a = 10
    var b = 5


    fmt.Println("Addition:", a+b)
    fmt.Println("Subtraction:", a-b)
    fmt.Println("Multiplication:", a*b)
    fmt.Println("Division:", a/b)
    fmt.Println("Modulus:", a%b)


    // Increment and Decrement
    a++
    b--
    fmt.Println("Increment:", a)
    fmt.Println("Decrement:", b)
}


Output:


Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0
Increment: 11
Decrement: 4


Relational Operators:


Relational operators compare operands and return a boolean value (true or false) based on the comparison. Here are the relational operators in Go:

  • Equal to (==): Checks if two operands are equal.
  • Not equal to (!=): Checks if two operands are not equal.
  • Greater than (>): Checks if the left operand is greater than the right operand.
  • Less than (<): Checks if the left operand is less than the right operand.
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.


Let's see an example that demonstrates the use of relational operators:


package main


import "fmt"


func main() {
    var a = 10
    var b = 5


    fmt.Println("Equal to:", a == b)
    fmt.Println("Not equal to:", a != b)
    fmt.Println("Greater than:", a > b)
    fmt.Println("Less than:", a < b)
    fmt.Println("Greater than or equal to:", a >= b)
    fmt.Println("Less than or equal to:", a <= b)
}


Output:


Equal to: false
Not equal to: true
Greater than: true
Less than: false
Greater than or equal to: true
Less than or equal to: false


Logical Operators:


Logical operators are used to perform logical operations on boolean operands. Go supports the following logical operators:

  • Logical AND (&&): Returns true if both operands are true.
  • Logical OR (||): Returns true if either operand is true.
  • Logical NOT (!): Negates the boolean value of an operand.


Let's explore an example that showcases the logical operators:


package main


import "fmt"


func main() {
    var x = true
    var y = false


    fmt.Println("Logical AND:", x && y)
    fmt.Println("Logical OR:", x || y)
    fmt.Println("Logical NOT:", !x)
}


Output:


Logical AND: false
Logical OR: true
Logical NOT: false


Conclusion:

In this blog post, we covered the basics of operators and expressions in Go programming. We explored arithmetic operators, relational operators, and logical operators, along with practical examples to demonstrate their usage. Understanding and effectively utilizing operators and expressions is crucial for manipulating data, making comparisons, and controlling the flow of your Go programs. Now that you have a solid understanding of these fundamental concepts, you're ready to dive deeper into Go programming and explore more advanced topics. Happy coding!

Next

Comments


🌟 Be the First to Share Your Thoughts! 🌟

Post a Comment

Address

Nashik, Maharastra (India)

Website
Site : www.anucodes.com
Social