SoFunction
Updated on 2025-03-05

Detailed explanation of go operator operation examples on variables and values

Operators are used to perform operations on variables and values

The plus operator (+) adds two values

As shown in the following example:

Sample code:

package main
import (
  "fmt"
)
func main() {
  var a = 15 + 25
  (a)
}

Although the plus operator is usually used to add two values, it can also be used to add variables and values, or to add one variable to another.

Sample code:

package main
import (
  "fmt"
)
func main() {
  var (
    sum1 = 100 + 50   // 150 (100 + 50)
    sum2 = sum1 + 250 // 400 (150 + 250)
    sum3 = sum2 + sum2 // 800 (400 + 400)
  )
  (sum3)
}

Arithmetic operators

Arithmetic operators are used to perform common mathematical operations.

  • +: Add, add two values, such as x + y
  • -: Subtraction, subtract another value from one value, such as x - y
  • *: Multiplication, multiply two values, such as x * y
  • /: division, divide one value by another, like x/y
  • %: Modular operation, return the division remainder, such as x % y
  • ++: Increase the value of the variable by 1, such as x++
  • --: Decrease the value of the variable by 1, such as x--
    Multiply 10 by 5 and print the result.
package main
import (
  "fmt"
)
func main() {
  (10 * 5)
}

Assignment operator

The assignment operator is used to assign values ​​to variables. In the following example, we assign the value 10 to a variable named x using the assignment operator (=):

package main
import (
  "fmt"
)
func main() {
  var x = 10
  (x)
}

The addition assignment operator (+=) adds a value to a variable:

package main
import (
  "fmt"
)
func main() {
  var x = 10
  x += 5
  (x)
}

List of all assignment operators:

  • =: Assign, such as x = 5
  • +=: Addition assignment, such as x += 3 (equivalent to x = x + 3)
  • -=: Subtraction assignment, such as x -= 3 (equivalent to x = x - 3)
  • *=: Multiplication assignment, such as x = 3 (equivalent to x = x 3)
  • /=: Division assignment, such as x /= 3 (equivalent to x = x / 3)
  • %=: Modal assignment, such as x %= 3 (equivalent to x = x % 3)
  • &=: bitwise and assignment, such as x &= 3 (equivalent to x = x & 3)
  • |=: bitwise or assign value, such as x |= 3 (equivalent to x = x | 3)
  • ^=: Bitwise XOR assignment, such as x^= 3 (equivalent to x = x^3)
  • >>=: Signed right shift assignment, such as x >>= 3 (equivalent to x = x >> 3)
  • <<=: left shift assignment, such as x <<= 3 (equivalent to x = x << 3)

Comparison operator

The comparison operator is used to compare two values. Note: The return value of the comparison is either true(1) or false(0). In the following example, we use the greater than operator (>) to determine whether 5 is greater than 3:

package main
import (
  "fmt"
)
func main() {
  var x = 5
  var y = 3
  (x &gt; y) // Return 1 (true) because 5 is greater than 3}

List of all comparison operators:

  • ==: equal to, such as x == y
  • !=: Not equal, such as x != y
  • >: greater than, such as x > y
  • <: less than, such as x < y
  • >=: greater than or equal to, such as x >= y
  • <=: less than or equal to, such as x <= y

Logical operators

Logical operators are used to determine the logical relationship between variables or values:

  • &&: Logical and, if both statements are true, returns true, such as x < 5 && x < 10
  • ||: logical or, if one of the statements is true, returns true, such as x < 5 || x < 4
  • !: Logical non, reverse the result, if the result is true, return false, such as !(x < 5 && x < 10)

bit operator

Bit operators are used to process binary numbers:

  • &: As operation, if both bits are 1, set the result bit to 1, such as x & y
  • |:or operation, if at least one of the two bits is 1, set the result bit to 1, such as x | y
  • ^: Exclusive OR operation. If only one bit is 1, set the result bit to 1, such as x^y
  • <<: Move left, shift left by adding zeros from the right, such as x << 2
  • >>: Move right, move right by removing bits from the left, such as x >> 2

The above is a detailed explanation of the examples of go operators performing operations on variables and values. For more information about go operators operating variable values, please pay attention to my other related articles!