Algorithm


Go programming language has a variety of operators that perform different operations on operands. Here's an overview of some commonly used operators in Go:

Arithmetic Operators:

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

Relational Operators:

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

Logical Operators:

  • && : Logical AND
  • || : Logical OR
  • ! : Logical NOT

Bitwise Operators:

  • & : Bitwise AND
  • | : Bitwise OR
  • ^ : Bitwise XOR
  • << : Left shift
  • >> : Right shift
  • &^ : Bitwise AND NOT

Assignment Operators:

  • = : Assign
  • += : Add and assign
  • -= : Subtract and assign
  • *= : Multiply and assign
  • /= : Divide and assign
  • %= : Modulus and assign
  • <<= : Left shift and assign
  • >>= : Right shift and assign
  • &= : Bitwise AND and assign
  • |= : Bitwise OR and assign
  • ^= : Bitwise XOR and assign
  • &^= : Bitwise AND NOT and assign

Other Operators:

  • & : Address of
  • * : Pointer dereference
  • <- : Channel send/receive

 

Code Examples

#1 Addition, Subtraction and Multiplication Operators in Go Lang programming

Code - Golang Programming

package main
import "fmt"

func main() {

  num1 := 6
  num2 := 2

  // + adds two variables
  sum := num1 + num2
  fmt.Printf("%d + %d = %d\n", num1, num2, sum)

  // - subtract two variables
  difference := num1 - num2
  fmt.Printf("%d - %d = %d\n",num1, num2,  difference)

  // * multiply two variables
  product := num1 * num2
  fmt.Printf("%d * %d is %d\n",num1, num2,  product)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
6 + 2 = 8
6 - 2 = 4
6 * 2 = 12

#2 Golang Division Operator in Go Lang Programming

Code - Golang Programming

package main
import "fmt"

func main() {

  num1 := 11
  num2 := 4

  // / divide two integer variables
  quotient := num1 / num2
  fmt.Printf(" %d / %d = %d\n", num1, num2, quotient)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
11 / 4 = 2

#3 Code Example with Golang Programming

Code - Golang Programming

package main
import "fmt"

func main() {

  num1 := 11.0
  num2 := 4.0

  // / divide two floating point variables
  result := num1 / num2
  fmt.Printf(" %g / %g = %g\n", num1, num2, result)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
11 / 4 = 2.75

#4 Modulus Operator in Go Lang Programming

Code - Golang Programming

package main
import "fmt"

func main() {

  num1 := 11
  num2 := 4

  // % modulo-divides two variables
  remainder := num1 % num2
  fmt.Println(remainder )

}
Copy The Code & Try With Live Editor

#5 Increment and Decrement Operator in Go Lang Programming

Code - Golang Programming

package main
import "fmt"

func main() {

  num := 5

  // increment of num by 1
  num++
  fmt.Println(num)  // 6

  // decrement of num by 1
  num--
  fmt.Println(num)  // 4

}
Copy The Code & Try With Live Editor

#6 Assignment Operator in Go Lang Programming

Code - Golang Programming

package main
import "fmt"

func main() {

  num := 6
  var result int

  // = operator to assign the value of num to result
  result = num
  fmt.Println(result)    // 6
}
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Go Operators-Go Lang Programming