Development with Go
- Manjitsing K. Valvi
Operators
Arithmetic operators
Operation Operator
Addition +
Subtraction -
Multiplication *
Division /
Modulus %
Increment ++
Decrement --
Operation Operator
Logical AND &&
Logical OR ||
Logical NOT !
Logical operators
Comparison operators
Operation Operator
Equal ==
Not equal !=
Less than <
Less than or equal to <=
Greater than >
Greater than or equal to >=
Decrement --
Assignment operators
= , += , -=, *= , /=, %=
Bitwise operators
Operation Operator Description
AND & Sets each bit to 1 if both bits are 1
OR | Sets each bit to 1 if one of two bits is 1
XOR ^ Sets each bit to 1 if only one of two bits is 1
Left shift <<
Shift left by pushing zeros in from the right and let the
leftmost bits fall off
Right shift >>
Shift right by pushing copies of the leftmost bit in from
the left, and let the rightmost bits fall of
● work only on integer variables having bit-patterns of equal length
var x, y = 3, 4
fmt.Printf("x + y = %dn", x+y) // Addition 7
fmt.Printf("x - y = %dn", x-y) // Subtraction -1
fmt.Printf("x * y = %dn", x*y) // Multiplication 12
fmt.Printf("x / y = %dn", x/y) // Division 0
fmt.Printf("x mod y = %dn", x%y) // Modulus 3
x++ // Increment only post increment and
// written separately, not allowed in
// Printf 4
fmt.Println(x == y) // Relational EQUAL TO false
fmt.Println(x != y) // Relational NOT EQUAL TO true
fmt.Println(x < y && x > z) // Logical AND false
fmt.Println(x < y || x > z) // Logical OR true
fmt.Println(!(x == y && x > z)) // Logical NOT true
fmt.Println("x & y =",x&y) // Bitwise AND 0
fmt.Println("x | y =", z) // Bitwise OR 15
fmt.Println("x ^ y =", z) // Bitwise XOR 15
fmt.Println("x << 1 =", z) // Bitwise Left shift 24
fmt.Println("x >> 1 =", z) // Bitwise Right shift 6
// var x1 uint=4
// z1 = x1 & y //ERROR Types not same
// Also
// var x1 float64=4.3
// z1 = x1 & y //ERROR Types not same
// Since Bitwise operators work only on integer variables having
// bit-patterns of equal length
References
● “The Go Programming Language ”,Alan A.A.Donovan,Brian W.Kernighan,Addison Wesley Publication

Operators

  • 1.
    Development with Go -Manjitsing K. Valvi
  • 2.
    Operators Arithmetic operators Operation Operator Addition+ Subtraction - Multiplication * Division / Modulus % Increment ++ Decrement -- Operation Operator Logical AND && Logical OR || Logical NOT ! Logical operators
  • 3.
    Comparison operators Operation Operator Equal== Not equal != Less than < Less than or equal to <= Greater than > Greater than or equal to >= Decrement -- Assignment operators = , += , -=, *= , /=, %=
  • 4.
    Bitwise operators Operation OperatorDescription AND & Sets each bit to 1 if both bits are 1 OR | Sets each bit to 1 if one of two bits is 1 XOR ^ Sets each bit to 1 if only one of two bits is 1 Left shift << Shift left by pushing zeros in from the right and let the leftmost bits fall off Right shift >> Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall of ● work only on integer variables having bit-patterns of equal length
  • 5.
    var x, y= 3, 4 fmt.Printf("x + y = %dn", x+y) // Addition 7 fmt.Printf("x - y = %dn", x-y) // Subtraction -1 fmt.Printf("x * y = %dn", x*y) // Multiplication 12 fmt.Printf("x / y = %dn", x/y) // Division 0 fmt.Printf("x mod y = %dn", x%y) // Modulus 3 x++ // Increment only post increment and // written separately, not allowed in // Printf 4 fmt.Println(x == y) // Relational EQUAL TO false fmt.Println(x != y) // Relational NOT EQUAL TO true fmt.Println(x < y && x > z) // Logical AND false fmt.Println(x < y || x > z) // Logical OR true fmt.Println(!(x == y && x > z)) // Logical NOT true
  • 6.
    fmt.Println("x & y=",x&y) // Bitwise AND 0 fmt.Println("x | y =", z) // Bitwise OR 15 fmt.Println("x ^ y =", z) // Bitwise XOR 15 fmt.Println("x << 1 =", z) // Bitwise Left shift 24 fmt.Println("x >> 1 =", z) // Bitwise Right shift 6 // var x1 uint=4 // z1 = x1 & y //ERROR Types not same // Also // var x1 float64=4.3 // z1 = x1 & y //ERROR Types not same // Since Bitwise operators work only on integer variables having // bit-patterns of equal length
  • 7.
    References ● “The GoProgramming Language ”,Alan A.A.Donovan,Brian W.Kernighan,Addison Wesley Publication