Module: Go Basics

Operators

Go Programming: Operators

Go provides a rich set of operators to perform various operations on data. Here's a breakdown of the different types of operators in Go, categorized for clarity.

1. Arithmetic Operators

These operators perform mathematical calculations.

Operator Description Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus (remainder) x % y
++ Increment (adds 1 to the operand) x++
-- Decrement (subtracts 1 from the operand) x--

Example:

package main

import "fmt"

func main() {
	x := 10
	y := 3

	fmt.Println("x + y =", x + y)     // Output: x + y = 13
	fmt.Println("x - y =", x - y)     // Output: x - y = 7
	fmt.Println("x * y =", x * y)     // Output: x * y = 30
	fmt.Println("x / y =", x / y)     // Output: x / y = 3
	fmt.Println("x % y =", x % y)     // Output: x % y = 1

	x++
	fmt.Println("x++ =", x)          // Output: x++ = 11

	y--
	fmt.Println("y-- =", y)          // Output: y-- = 2
}

2. Relational Operators (Comparison Operators)

These operators compare two values and return a boolean (true or false).

Operator Description Example
== Equal to x == y
!= Not equal to x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Example:

package main

import "fmt"

func main() {
	x := 5
	y := 10

	fmt.Println("x == y:", x == y)   // Output: x == y: false
	fmt.Println("x != y:", x != y)   // Output: x != y: true
	fmt.Println("x > y:", x > y)     // Output: x > y: false
	fmt.Println("x < y:", x < y)     // Output: x < y: true
	fmt.Println("x >= y:", x >= y)   // Output: x >= y: false
	fmt.Println("x <= y:", x <= y)   // Output: x <= y: true
}

3. Logical Operators

These operators combine or modify boolean expressions.

Operator Description Example
&& Logical AND (returns true if both operands are true) x && y
` `
! Logical NOT (inverts the boolean value of the operand) !x

Example:

package main

import "fmt"

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

	fmt.Println("x && y:", x && y)   // Output: x && y: false
	fmt.Println("x || y:", x || y)   // Output: x || y: true
	fmt.Println("!x:", !x)          // Output: !x: false
}

4. Bitwise Operators

These operators operate on the individual bits of integer operands.

Operator Description Example
& Bitwise AND x & y
` ` Bitwise OR
^ Bitwise XOR x ^ y
&^ Bitwise AND NOT x &^ y
<< Left shift x << n (shifts bits of x to the left by n positions)
>> Right shift x >> n (shifts bits of x to the right by n positions)

Example:

package main

import "fmt"

func main() {
	x := 5  // Binary: 0101
	y := 3  // Binary: 0011

	fmt.Println("x & y:", x & y)   // Output: x & y: 1 (Binary: 0001)
	fmt.Println("x | y:", x | y)   // Output: x | y: 7 (Binary: 0111)
	fmt.Println("x ^ y:", x ^ y)   // Output: x ^ y: 6 (Binary: 0110)
	fmt.Println("x << 1:", x << 1) // Output: x << 1: 10 (Binary: 1010)
	fmt.Println("x >> 1:", x >> 1) // Output: x >> 1: 2 (Binary: 0010)
}

5. Assignment Operators

These operators assign values to variables.

Operator Description Example
= Simple assignment x = 10
+= Add and assign x += 5 (equivalent to x = x + 5)
-= Subtract and assign x -= 5 (equivalent to x = x - 5)
*= Multiply and assign x *= 5 (equivalent to x = x * 5)
/= Divide and assign x /= 5 (equivalent to x = x / 5)
%= Modulus and assign x %= 5 (equivalent to x = x % 5)
&= Bitwise AND and assign x &= 5 (equivalent to x = x & 5)
` =` Bitwise OR and assign
^= Bitwise XOR and assign x ^= 5 (equivalent to x = x ^ 5)
<<= Left shift and assign x <<= 5 (equivalent to x = x << 5)
>>= Right shift and assign x >>= 5 (equivalent to x = x >> 5)

Example:

package main

import "fmt"

func main() {
	x := 10

	x += 5
	fmt.Println("x += 5:", x) // Output: x += 5: 15

	x -= 3
	fmt.Println("x -= 3:", x) // Output: x -= 3: 12

	x *= 2
	fmt.Println("x *= 2:", x) // Output: x *= 2: 24
}

6. Other Operators

  • . (Dot Operator): Used to access fields and methods of a struct.
  • -> (Arrow Operator): Used to access fields and methods of a struct through a pointer. (Less common in modern Go, prefer using the dot operator with pointer receivers).
  • := (Short Variable Declaration): Declares and initializes a variable. Only usable inside functions.
  • ... (Ellipsis): Used in function calls to pass a variable number of arguments, and in array/slice literals to create arrays/slices of variable length.

This comprehensive overview should give you a solid understanding of the operators available in Go. Remember to consider operator precedence when writing complex expressions. You can find the full precedence table in the official Go documentation: https://go.dev/ref/spec#Operators_and_precedence