1 Overview of Golang Operators

In the Go language, operators are used to perform program code operations, including arithmetic operations, relational comparisons, logical operations, and more. Different operators correspond to different functionalities and usage scenarios. Understanding and mastering these operators is the foundation for effective programming.

2 Arithmetic Operators

Go language provides a set of arithmetic operators:

  • Addition (+): Addition operation
  • Subtraction (-): Subtraction operation
  • Multiplication (*): Multiplication operation
  • Division (/): Division operation
  • Remainder (%): Modulo operation

Examples:

package main

import "fmt"

func main() {
    // Addition operation
    sum := 10 + 5
    fmt.Println("10 + 5 =", sum) // Outputs 15

    // Subtraction operation
    diff := 10 - 5
    fmt.Println("10 - 5 =", diff) // Outputs 5

    // Multiplication operation
    prod := 10 * 5
    fmt.Println("10 * 5 =", prod) // Outputs 50

    // Division operation
    quot := 10 / 5
    fmt.Println("10 / 5 =", quot) // Outputs 2

    // Modulo operation
    rem := 10 % 5
    fmt.Println("10 % 5 =", rem) // Outputs 0
}

3 Logical Operators

Logical operators in Go language are used to connect multiple conditions (generally used for boolean values). It includes three types: AND (&&), OR (||), and NOT (!).

3.1 AND (&&)

When all conditions are true, the result of the "AND" operation is true.

Examples:

package main

import "fmt"

func main() {
    fmt.Println(true && true)   // Outputs true
    fmt.Println(true && false)  // Outputs false
    fmt.Println(false && false) // Outputs false
    // Result is true only when both conditions are true
}

3.2 OR (||)

If at least one condition is true, the result of the "OR" operation is true.

Examples:

package main

import "fmt"

func main() {
    fmt.Println(true || true)   // Outputs true
    fmt.Println(true || false)  // Outputs true
    fmt.Println(false || false) // Outputs false
    // Result is true if at least one condition is true
}

3.3 NOT (!)

Used to reverse the boolean value of a condition.

Examples:

package main

import "fmt"

func main() {
    fmt.Println(!true)  // Outputs false
    fmt.Println(!false) // Outputs true
    // Reverses the boolean value
}

4 Comparison Operators

Comparison operators in Go language are used to compare two variables or values:

  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

The result of comparison operations is a boolean value: true or false.

5 Bitwise Operators

In Go, bitwise operators allow us to directly manipulate the binary bits of data:

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise left shift (<<)
  • Bitwise right shift (>>)

Bitwise operators are mainly used in low-level programming, such as direct hardware operations, encryption, and other scenarios.

6 Assignment Operators

Assignment operators are used to assign values to variables. In Go language, there are multiple forms of assignment operators:

  • Simple assignment (=)
  • Addition assignment (+=)
  • Subtraction assignment (-=)
  • Multiplication assignment (*=)
  • Division assignment (/=)
  • Modulus assignment (%=)

These operators make the code more concise. For example, x += 1 is equivalent to x = x + 1.

7 Special Operators

7.1 Increment (++) and Decrement (--) Operators

The increment operator (++) and decrement operator (--) are used to add 1 and subtract 1 from the value of a variable, respectively. They are the only operators in the Go language that can act directly on the variable itself (not on an expression).

Example:

package main

import "fmt"

func main() {
    x := 1
    x++
    fmt.Println("x++ =", x) // Outputs 2

    y := 10
    y--
    fmt.Println("y-- =", y) // Outputs 9
}

Note that in Go language, x++ or x-- cannot be a part of an expression. For example, z := x++ is not allowed.

7.2 Type Conversion

Go language is statically typed, which means once a variable is declared, its type cannot be changed. Therefore, sometimes it is necessary to convert a variable of one type to another type. Go does not have automatic type conversion; explicit type conversion is required.

Example:

package main

import (
	"fmt"
)

func main() {
	var i int = 42
	fmt.Println(float64(i)) // Convert int to float64

	var f float64 = 3.14
	fmt.Println(int(f)) // Convert float64 to int, decimal part will be discarded
}

When performing type conversion, it is important to ensure that the data is safe during the conversion process, such as the potential loss of precision when converting a floating-point number to an integer.