Go Operators

Welcome to The Coding College!
Operators are essential in any programming language as they perform actions like calculations, comparisons, logical evaluations, and more. In Go (Golang), operators are categorized based on their functionality. This guide will cover all types of operators in Go with syntax, examples, and use cases.

What Are Operators in Go?

An operator is a symbol or keyword used to perform operations on variables and values. For example:

result := 5 + 3 // '+' is an operator for addition

Types of Operators in Go

Go provides the following categories of operators:

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Miscellaneous Operators

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

OperatorDescriptionExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division6 / 32
%Modulus (remainder)5 % 32

Example: Arithmetic Operators

package main

import "fmt"

func main() {
    a, b := 10, 3
    fmt.Println("Addition:", a+b)        // Output: 13
    fmt.Println("Subtraction:", a-b)     // Output: 7
    fmt.Println("Multiplication:", a*b) // Output: 30
    fmt.Println("Division:", a/b)       // Output: 3
    fmt.Println("Modulus:", a%b)        // Output: 1
}

2. Relational (Comparison) Operators

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

OperatorDescriptionExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
<Less than3 < 5true
<=Less than or equal to5 <= 5true
>Greater than5 > 3true
>=Greater than or equal to5 >= 3true

Example: Relational Operators

package main

import "fmt"

func main() {
    x, y := 10, 20
    fmt.Println("x == y:", x == y) // Output: false
    fmt.Println("x != y:", x != y) // Output: true
    fmt.Println("x < y:", x < y)   // Output: true
    fmt.Println("x > y:", x > y)   // Output: false
}

3. Logical Operators

Logical operators are used to combine or invert boolean conditions.

OperatorDescriptionExampleResult
&&Logical ANDtrue && falsefalse
``Logical OR
!Logical NOT!truefalse

Example: Logical Operators

package main

import "fmt"

func main() {
    a, b := true, false
    fmt.Println("a && b:", a && b) // Output: false
    fmt.Println("a || b:", a || b) // Output: true
    fmt.Println("!a:", !a)         // Output: false
}

4. Bitwise Operators

Bitwise operators perform operations on binary representations of numbers.

OperatorDescriptionExampleResult
&Bitwise AND5 & 31
``Bitwise OR`5
^Bitwise XOR5 ^ 36
<<Left shift2 << 14
>>Right shift4 >> 12

Example: Bitwise Operators

package main

import "fmt"

func main() {
    x, y := 5, 3
    fmt.Println("x & y:", x&y)   // Output: 1
    fmt.Println("x | y:", x|y)   // Output: 7
    fmt.Println("x ^ y:", x^y)   // Output: 6
    fmt.Println("x << 1:", x<<1) // Output: 10
    fmt.Println("x >> 1:", x>>1) // Output: 2
}

5. Assignment Operators

Assignment operators are used to assign and modify values of variables.

OperatorDescriptionExampleEquivalent
=Assignx = 5x = 5
+=Add and assignx += 3x = x + 3
-=Subtract and assignx -= 3x = x - 3
*=Multiply and assignx *= 3x = x * 3
/=Divide and assignx /= 3x = x / 3
%=Modulus and assignx %= 3x = x % 3

Example: Assignment Operators

package main

import "fmt"

func main() {
    x := 10
    x += 5
    fmt.Println("x += 5:", x) // Output: 15
    x *= 2
    fmt.Println("x *= 2:", x) // Output: 30
}

6. Miscellaneous Operators

Address Operator (&)

Used to get the memory address of a variable.

x := 10
fmt.Println(&x) // Output: Memory address of x

Dereference Operator (*)

Used to access the value at a memory address.

ptr := &x
fmt.Println(*ptr) // Output: 10

Conclusion

Operators are the backbone of any program, enabling logical, arithmetic, and data manipulation. By mastering operators in Go, you can write concise, efficient, and powerful code.

Leave a Comment