Go Operators

In this tutorial, we will learn about the different types of operators in the Go programming language (Golang). Operators are special symbols or keywords used to perform operations on variables and values. Go provides a rich set of operators to work with arithmetic, logical, relational, and other operations. We will explore these operators with examples for better understanding.


Types of Operators in Go

  1. Arithmetic Operators
  2. Relational 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:

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder)
Arithmetic Operators

Example

</>
Copy
package main

import "fmt"

func main() {
    a := 10
    b := 3

    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)
}

Arithmetic Operators

  1. Go – Addition Operator
  2. Go – Subtraction Operator
  3. Go – Multiplication Operator
  4. Go – Division Operator
  5. Go – Remainder Operator
  6. Go – Increment Operator
  7. Go – Decrement Operator

2. Relational Operators

Relational operators are used to compare two values and return a boolean result:

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
Relational Operators

Example

</>
Copy
package main

import "fmt"

func main() {
    a := 10
    b := 20

    fmt.Println("a == b:", a == b)
    fmt.Println("a != b:", a != b)
    fmt.Println("a > b:", a > b)
    fmt.Println("a < b:", a < b)
    fmt.Println("a >= b:", a >= b)
    fmt.Println("a <= b:", a <= b)
}

3. Logical Operators

Logical operators are used to combine conditional statements:

OperatorDescription
&&Logical AND
||Logical OR
!Logical NOT
Logical Operators

Example

</>
Copy
package main

import "fmt"

func main() {
    a := true
    b := false

    fmt.Println("a && b:", a && b)
    fmt.Println("a || b:", a || b)
    fmt.Println("!a:", !a)
}

Logical Operators


4. Bitwise Operators

Bitwise operators perform bit-level operations:

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
<<Left shift
>>Right shift
Bitwise Operators

Example

</>
Copy
package main

import "fmt"

func main() {
    a := 5  // Binary: 0101
    b := 3  // Binary: 0011

    fmt.Println("a & b:", a&b)
    fmt.Println("a | b:", a|b)
    fmt.Println("a ^ b:", a^b)
    fmt.Println("a << 1:", a<<1)
    fmt.Println("a >> 1:", a>>1)
}

5. Assignment Operators

Assignment operators are used to assign values to variables:

OperatorDescription
=Assign
+=Add and assign
-=Subtract and assign
*=Multiply and assign
/=Divide and assign
%=Modulus and assign
Assignment Operators

Example

</>
Copy
package main

import "fmt"

func main() {
    a := 10

    a += 5
    fmt.Println("a += 5:", a)

    a -= 3
    fmt.Println("a -= 3:", a)

    a *= 2
    fmt.Println("a *= 2:", a)

    a /= 4
    fmt.Println("a /= 4:", a)

    a %= 3
    fmt.Println("a %= 3:", a)
}

6. Miscellaneous Operators

OperatorDescription
&Address of a variable
*Pointer to a variable
Miscellaneous Operators

Conclusion

Operators are fundamental to any programming language, and Go provides a rich set of operators to perform a wide range of operations. Understanding these operators will help you write efficient and concise code in your programs.