Press "Enter" to skip to content

Golang Basic 4 - Basic Operators

Golang Basic 4 - Basic Operators

Arithmetic Operators

Operators

Operator Sense Description Sample Result
Addition The ‘ ’ operator adds two operands. 3 2 5
- Subtraction The ‘-‘ operator subtracts two operands. 3 - 2 1
* Multiplication The ‘*’ operator multiplies two operands. 3 * 2 6
/ Division The ‘/’ operator divides the first operand by the second. 3 / 2 1
% Modulus The ‘%’ operator returns the remainder when first operand is divided by the second. 3 % 2 1

Sample

usr@host:go/ # cat l4s1.go
package main
import (
    "fmt"
)
func main(){
    var var1 int
    var var2 int
    var1 = 3
    var2 = 2
    fmt.Printf("%d   %d = %d\n", var1, var2 , var1   var2)
    fmt.Printf("%d - %d = %d\n", var1, var2 , var1 - var2)
    fmt.Printf("%d * %d = %d\n", var1, var2 , var1 * var2)
    fmt.Printf("%d / %d = %d\n", var1, var2 , var1 / var2)
    fmt.Printf("%d %% %d = %d\n", var1, var2 , var1 % var2)
}
usr@host:go/ # go run l4s1.go
3   2 = 5
3 - 2 = 1
3 * 2 = 6
3 / 2 = 1
3 % 2 = 1

Relational Operators

Operators

Operator Sense Description Sample Result
== Equal To Operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. 1 == 3 false
!= Not Equal To Operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. 1 != 3 true
> Greater Than Operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. 1 > 3 false
< Less Than Operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. 1 < 3 true
>= Greater Than Equal To Operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. 1 >= 3 false
<= Less Than Equal To Operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. 1 <= 3 true

Sample

usr@host:go/ # cat l4s2.go
package main
import (
    "fmt"
)
func main(){
    var var1 int
    var var2 int
    var1 = 1
    var2 = 3
    fmt.Printf("%t\n", var1 == var2)
    fmt.Printf("%t\n", var1 != var2)
    fmt.Printf("%t\n", var1 > var2)
    fmt.Printf("%t\n", var1 < var2)
    fmt.Printf("%t\n", var1 >= var2)
    fmt.Printf("%t\n", var1 <= var2)
}
usr@host:go/ # go run l4s2.go
false
true
false
true
false
true

Logical Operators

Operators

Operator Sense Description Sample Result
&& Logical AND The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. true && false false
|| Logical OR The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. true || false true
! Logical NOT The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. !true false

Sample

usr@host:go/ # cat l4s3.go
package main
import (
    "fmt"
)
func main(){
    var flag1 bool = true
    var flag2 bool = false
    fmt.Printf("%t\n", flag1 && flag2)
    fmt.Printf("%t\n", flag1 || flag2)
    fmt.Printf("%t\n", !flag1)
}
usr@host:go/ # go run l4s3.go
false
true
false

Bitwise Operators

Operators

Operator Sense Description Sample Result
& bitwise AND Takes two numbers as operands and does AND on every bit of two numbers. 1 & 2 0
| bitwise OR Takes two numbers as operands and does OR on every bit of two numbers. 1 | 2 3
^ bitwise XOR Takes two numbers as operands and does XOR on every bit of two numbers. 1 ^ 2 3
<< left shift Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. 1 << 2 4
>> right shift Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift. 1 >> 2 0
&^ AND NOT This is a bit clear operator. 1 &^ 2 1

Sample

usr@host:go/ # cat l4s4.go
package main
import (
    "fmt"
)
func main(){
    var var1 int = 1
    var var2 int = 2
    fmt.Printf("%d = %04b\n", var1, var1)
    fmt.Printf("%d = %04b\n", var2, var2)
    fmt.Printf("%d : %04b\n", var1 & var2, var1 & var2)
    fmt.Printf("%d : %04b\n", var1 | var2, var1 | var2)
    fmt.Printf("%d : %04b\n", var1 ^ var2, var1 ^ var2)
    fmt.Printf("%d : %04b\n", var1 << var2, var1 << var2)
    fmt.Printf("%d : %04b\n", var1 >> var2, var1 >> var2)
    fmt.Printf("%d : %04b\n", var1 &^ var2, var1 &^ var2)
}
usr@host:go/ # go run l4s4.go
1 = 0001
2 = 0010
0 : 0000
3 : 0011
3 : 0011
4 : 0100
0 : 0000
1 : 0001

Assignment Operators

Operators

Operator Sense Description Sample Result
= Simple Assignment This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. var1 = 1 1
= Add Assignment This operator is combination of ‘ ’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. 1 = 1 2
-= Subtract Assignment This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left. 2 -= 1 1
*= Multiply Assignment This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. 1 *= 2 2
/= Division Assignment This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. 2 /= 2 1
%= Modulus Assignment This operator is combination of ‘%’ and ‘=’ operators. This operator first modulo the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. 1 %= 2 1
&= Bitwise AND Assignment This operator is combination of ‘&’ and ‘=’ operators. This operator first “Bitwise AND” the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left. 1 &= 2 0
|= Bitwise Inclusive OR This operator is combination of ‘|’ and ‘=’ operators. This operator first “Bitwise Inclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. 0 |= 2 2
^= Bitwise Exclusive OR This operator is combination of ‘ and ‘=’ operators. This operator first “Bitwise Exclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. 2 ^= 2 0
  • You can use ' ', '--', after the variable name, to quickly 1/-1;

Sample

usr@host:go/ # cat l4s5.go
package main
import (
    "fmt"
)
func main(){
    var var1 int
    fmt.Printf("%d\n", var1)
    var1 = 1
    fmt.Printf("%d\n", var1)
    var1  = 1
    fmt.Printf("%d\n", var1)
    var1 -= 1
    fmt.Printf("%d\n", var1)
    var1 *= 2
    fmt.Printf("%d\n", var1)
    var1 /= 2
    fmt.Printf("%d\n", var1)
    var1 %= 2
    fmt.Printf("%d\n", var1)
    var1 &= 2
    fmt.Printf("%d\n", var1)
    var1 |= 2
    fmt.Printf("%d\n", var1)
    var1 ^= 2
    fmt.Printf("%d\n", var1)
}
usr@host:go/ # go run l4s5.go
1
2
1
2
1
1
2

Misc Operators

Operators

Operator Sense Description
& Get Address This operator returns the address of the variable.
* Get Pointer's Value This operator provides pointer to a variable.
<- Receive a value from the channel The name of this operator is receive. It is used to receive a value from the channel.

Sample

usr@host:go/ # cat l4s6.go
package main
import (
    "fmt"
)
func main(){
    var var1 int = 0
    fmt.Printf("value : %d\n", *&var1)
    fmt.Printf("physical : %p\n", &var1)
}
usr@host:go/ # go run l4s6.go
value : 0
physical : 0xc0000180c0

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *