Go Operators

Master the art of Go programming with an exploration of Go operators. Learn how to optimize your code using arithmetic, comparison, and logical operators
E
Edtoks11:41 min read

What is an Operator?

In programming, an operator is a symbol that represents a specific operation to be performed on one or more operands. Operators define how variables or values are manipulated to produce a result.

What is the Operator in Go?

In Go, operators are fundamental building blocks that allow developers to perform various operations on variables and values. These operations range from basic arithmetic calculations to more complex bitwise manipulations and logical evaluations.

Different Types of Operators

Arithmetic Operators

Arithmetic operators are used for basic mathematical operations such as addition, subtraction, multiplication, and division.

package main
import "fmt"
func main() {

a, b := 10, 4
fmt.Println("Addition:", a+b)
fmt.Println("Subtraction:", a-b)
fmt.Println("Multiplication:", a*b)
fmt.Println("Division:", a/b)
}

Lis of Arithmetic Operators

Operator

Symbol

Description

Example

Addition

+

Adds two operands

result := 5 + 3

Subtraction

-

Subtracts the right operand from the left

result := 8 - 3

Multiplication

*

Multiplies two operands

result := 4 * 2

Division

/

Divides the left operand by the right

result := 10 / 2

Modulus

%

Returns the remainder of the division

result := 13 % 5

Assignment Operators

Assignment operators are used to assign values to variables. They can also combine assignments with arithmetic operations.

package main
import "fmt"
func main() {

x := 5

y := 10
x += y  // Equivalent to x = x + y
fmt.Println("Updated x:", x)
}

List of Assignment Operators

Operator

Symbol

Description

Example

Assignment

=

Assigns the value on the right to the variable on the left

x := 10

Addition Assignment

+=

Adds the right operand to the left and assigns the result to the left

x += 5

Subtraction Assignment

-=

Subtracts the right operand from the left and assigns the result to the left

x -= 3

Multiplication Assignment

*=

Multiplies the left operand by the right and assigns the result to the left

x *= 2

Division Assignment

/=

Divides the left operand by the right and assigns the result to the left

x /= 4

Comparison Operators

Comparison operators are used to compare values and return a boolean result.

package main
import "fmt"
func main() {

num1, num2 := 10, 20
fmt.Println("Equal:", num1 == num2)
fmt.Println("Not Equal:", num1 != num2)
fmt.Println("Greater Than:", num1 > num2)
fmt.Println("Less Than:", num1 < num2)
}

List of Comparison Operators

Operator

Symbol

Description

Example

Equal

==

Checks if the values of two operands are equal

result := (5 == 5)

Not Equal

!=

Checks if the values of two operands are not equal

result := (5 != 3)

Greater Than

>

Checks if the left operand is greater than the right

result := (8 > 3)

Less Than

<

Checks if the left operand is less than the right

result := (4 < 7)

Greater Than or Equal To

>=

Checks if the left operand is greater than or equal to the right

result := (10 >= 10)

Less Than or Equal To

<=

Checks if the left operand is less than or equal to the right

result := (5 <= 8)

Logical Operators

Logical operators perform logical operations on boolean values.

package main
import "fmt"
func main() {

isTrue, isFalse := true, false
fmt.Println(&#34;Logical AND:&#34;, isTrue &amp;&amp; isFalse)
fmt.Println(&#34;Logical OR:&#34;, isTrue || isFalse)
fmt.Println(&#34;Logical NOT:&#34;, !isTrue)
}

List of Logical Operators

Operator

Symbol

Description

Example

Logical AND

&&

Returns true if both operands are true

result := (true && false)

Logical OR

||

Returns true if at least one operand is true

result := (true || false)

Logical NOT

!

Returns true if the operand is false and vice versa

result := !true

Bitwise Operators

Bitwise operators manipulate individual bits of integer values.

package main
import "fmt"
func main() {

num1, num2 := 5, 3
fmt.Println(&#34;Bitwise AND:&#34;, num1&amp;num2)
fmt.Println(&#34;Bitwise OR:&#34;, num1|num2)
fmt.Println(&#34;Bitwise XOR:&#34;, num1^num2)
fmt.Println(&#34;Bitwise NOT:&#34;, ^num1)
}

List of Bitwise Operators

Operator

Symbol

Description

Example

Bitwise AND

&

Performs a bitwise AND operation on the operands

result := 5 & 3

Bitwise OR

|

Performs a bitwise OR operation on the operands

result := 5 | 3

Bitwise XOR

^

Performs a bitwise XOR operation on the operands

result := 5 ^ 3

Bitwise NOT

~

Performs a bitwise NOT operation on the operand

result := ^5

Left Shift

<<

Shifts the bits of the left operand to the left by the number of positions specified by the right operand

result := 5 << 2

Right Shift

>>

Shifts the bits of the left operand to the right by the number of positions specified by the right operand

result := 8 >> 2

List of Unary Operators

Unary operators operate on a single operand.

Operator

Symbol

Description

Example

Unary Plus

+

Indicates a positive value

result := +5

Unary Minus

-

Negates the value of the operand

result := -3

Logical NOT

!

Inverts the logical value of the operand

result := !true

Bitwise NOT

^

Inverts the bits of the operand

result := ^5

Operator Precedence in Go

In Go, operator precedence determines the order in which operators are evaluated within an expression. Operators with higher precedence are evaluated first. Here's a list of operator precedence in Go, from highest to lowest:

  1. Unary operators: +, -, !, ^ (highest precedence)

  2. Multiplicative operators: *, /, %

  3. Additive operators: +, -

  4. Bitwise shift operators: <<, >>

  5. Relational operators: <, <=, >, >=

  6. Equality operators: ==, !=

  7. Bitwise AND: &

  8. Bitwise XOR: ^

  9. Bitwise OR: |

  10. Logical AND: &&

  11. Logical OR: ||

  12. Conditional (Ternary) operator: ? :

  13. Assignment operators: =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |= (lowest precedence)

It's important to note that parentheses () can be used to override the default precedence and explicitly specify the order of evaluation in an expression.

Here's an example demonstrating the operator precedence in Go:

package main
import "fmt"
func main() {

result := 5 + 23 > 7 && !(4 == 4)

fmt.Println("Result:", result)

}

In this example, the multiplication () has higher precedence than addition (+), and the logical NOT (!) has higher precedence than the logical AND (&&). The result is calculated based on the operator precedence, producing the final boolean result.

Conclusion

Understanding the different types of operators in Go and their specific use cases is crucial for writing effective and expressive code. The examples provided illustrate how these operators are applied in various scenarios, from basic arithmetic to complex logical and bitwise operations. As you continue to explore and use Go in your programming projects, a solid understanding of operators will greatly enhance your ability to manipulate and control the flow of your code.

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.