Operators in Go

Operators are special symbols used to perform operations on variables and values. Go provides a variety of operators for mathematical, logical, and bitwise computations, as well as for assignment and comparison.

Types of Operators in Go

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

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

Operator Description Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2
% Modulus (Remainder) 10 % 3 1
++ Increment by 1 a++ if a = 10, becomes 11
-- Decrement by 1 a-- if a = 10, becomes 9

Example:

package main
import "fmt"

func main() {
    a, b := 10, 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)
}

2. Relational Operators

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

Operator Description Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true
> Greater than 5 > 3 true
< Less than 5 < 3 false
>= Greater than or equal to 5 >= 5 true
<= Less than or equal to 5 <= 8 true

Example:

x, y := 10, 20
fmt.Println(x > y)  // false
fmt.Println(x == y) // false
fmt.Println(x != y) // true

3. Logical Operators

Logical operators are used to combine multiple conditions.

Operator Description Example Result
&& Logical AND (5 > 3) && (10 > 5) true
` ` Logical OR `(5 > 10) (10 > 5)` true
! Logical NOT !(5 > 3) false

Example:

a, b := true, false
fmt.Println(a && b) // false
fmt.Println(a || b) // true
fmt.Println(!a)     // false

4. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example Equivalent To
= Assign a = 5 a = 5
+= Add and assign a += 3 a = a + 3
-= Subtract and assign a -= 3 a = a - 3
*= Multiply and assign a *= 3 a = a * 3
/= Divide and assign a /= 3 a = a / 3
%= Modulus and assign a %= 3 a = a % 3

Example:

a := 10
a += 5
fmt.Println(a) // 15

5. Bitwise Operators

Bitwise operators perform operations on bits (binary representations of numbers).

Operator Description Example Result
& Bitwise AND 5 & 3 1
` ` Bitwise OR `5 3` 7
^ Bitwise XOR 5 ^ 3 6
&^ Bit clear (AND NOT) 5 &^ 3 4
<< Left shift 5 << 1 10
>> Right shift 5 >> 1 2

Explanation:

5 = 0101
3 = 0011
--------------
5 & 3 = 0001 (1)
5 | 3 = 0111 (7)
5 ^ 3 = 0110 (6)

Example:

a, b := 5, 3
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)

6. Miscellaneous Operators

Operator Description Example Result
& Address of variable &a Returns memory address of a
* Pointer to a variable *ptr Accesses value stored at the address ptr
<- Channel operator (used for sending/receiving in concurrency) ch <- value or value := <-ch Sends or receives value through a channel

Example (Pointer):

var x = 10
var p = &x
fmt.Println("Address:", p)
fmt.Println("Value:", *p)

Summary: Go’s operators make it easy to perform mathematical, logical, and bitwise operations efficiently. They also support low-level memory manipulation through pointers and concurrency using channels.