Skip to main content

Command Palette

Search for a command to run...

Operators in Go

Updated
5 min read
Operators in Go

In Go, operators are symbols used to perform operations, such as arithmetic, logical, or bitwise operations. This lesson will cover different types of operators and their behaviour, along with examples and ASCII diagrams where needed.


Arithmetic Operators

Go supports the following arithmetic operators for both integers and floating-point numbers:

  • Addition: +

  • Subtraction: -

  • Multiplication: *

  • Division: /

  • Modulus: % (for integers only)

Integer Division Example

In Go, division between integers results in a floored integer result.

9 / 4 = 2   // Floored result, no decimal part

For floating-point numbers, division by zero results in +Inf, while integer division by zero causes a run-time panic.

Shortcut Assignment Operators

Go provides shortcut operators to simplify operations like:

  • b += a (equivalent to b = b + a)

  • b -= a (equivalent to b = b - a)

  • b *= a (equivalent to b = b * a)

  • b /= a (equivalent to b = b / a)

  • b %= a (equivalent to b = b % a)

Unary Increment and Decrement

Go supports the increment (++) and decrement (--) operators, but they can only be used as standalone statements (not expressions):

i++   // Equivalent to i = i + 1
i--   // Equivalent to i = i - 1

Logical Operators

Logical operators in Go are used to compare values or combine Boolean conditions. The most common logical operators are:

  • Equality (==): Returns true if both operands are equal.

  • Not-Equal (!=): Returns true if operands are different.

  • Less-than (<), Greater-than (>)

  • Less-than or equal (<=), Greater-than or equal (>=)

Example of Logical Comparison

5 > 3    // true
7 <= 5   // false
"abc" == "abc"   // true

In Go, logical comparisons are strict, meaning both operands must be of the same type (e.g., comparing an int with an int32 directly will result in a compiler error).

Boolean Logical Operators

Go also provides the following Boolean operators:

  • AND (&&): True if both operands are true.

  • OR (||): True if at least one operand is true.

  • NOT (!): Negates a Boolean value (i.e., true becomes false and vice versa).

true && false    // false
true || false    // true
!true            // false

These operators short-circuit. For example, in an && operation, if the left operand is false, the right operand is not evaluated.


Bitwise Operators

Bitwise operators in Go operate on the binary representation of integers.

Common Bitwise Operators

  • Bitwise AND (&)

  • Bitwise OR (|)

  • Bitwise XOR (^)

  • Bitwise AND NOT (&^) (clear bits)

  • Bitwise NOT (^) (complement)

Here's how they work using ASCII diagrams:

Bitwise AND (&)

The AND operator compares corresponding bits of two integers and returns 1 if both bits are 1; otherwise, it returns 0.

Example: 5 & 3

5 in binary:  0101
3 in binary:  0011
              ----
Result:       0001  (which is 1 in decimal)

Bitwise OR (|)

The OR operator compares corresponding bits and returns 1 if at least one of the bits is 1.

Example: 5 | 3

5 in binary:  0101
3 in binary:  0011
              ----
Result:       0111  (which is 7 in decimal)

Bitwise XOR (^)

The XOR operator returns 1 only if one of the bits is 1 and the other is 0.

Example: 5 ^ 3

5 in binary:  0101
3 in binary:  0011
              ----
Result:       0110  (which is 6 in decimal)

Bitwise AND NOT (&^)

This operator clears bits in the first operand where the second operand has 1 bits.

Example: 5 &^ 3

5 in binary:  0101
3 in binary:  0011
              ----
Result:       0100  (which is 4 in decimal)

Bitwise Shift Operators

  • Left Shift (<<): Shifts the bits to the left, effectively multiplying the number by powers of 2.

  • Right Shift (>>): Shifts the bits to the right, dividing the number by powers of 2.

Example of left shift by 2 (a << 2):

a = 5 in binary:  00000101
Shift left by 2:  00010100  (now a = 20 in decimal)

Example of right shift by 2 (a >> 2):

a = 20 in binary:  00010100
Shift right by 2:  00000101  (now a = 5 in decimal)

Operator Precedence

Operator precedence determines the order in which operations are performed. Operators with higher precedence are evaluated before operators with lower precedence. Here's a quick summary:

PrecedenceOperator(s)
7^, !
6*, /, %, <<, >>, &, &^
5+, -, `
4==, !=, <, <=, >, >=
3<-
2&&
1`

Parentheses can be used to override the default precedence, ensuring certain operations are evaluated first.

Example of Operator Precedence

result := 5 + 3 * 2   // Multiplication happens first
// result = 5 + (3 * 2) = 5 + 6 = 11

To change the order of evaluation, use parentheses:

result := (5 + 3) * 2   // Addition happens first
// result = (5 + 3) * 2 = 8 * 2 = 16

That's it for operators! Understanding how these operators work and their precedence will help you write more efficient and clear code. Now, get ready for the next challenge to test your knowledge!

21 views

Learn GO

Part 5 of 13

In this series, I will write articles that help you understand go! I am learning go so I am writing this series as a note for myself and hope you also love this series and learn along with me!

Up next

Elementary Types in Go

Go has three primary elementary types: Boolean, Numeric, and Character. These are fundamental building blocks used to represent different kinds of data in Go programs. Let's dive into each of them in detail. Boolean Type A Boolean represents one of ...