Go Operators
Go Language - Operators
A symbol which helps the compiler to perform specific logical or mathematical calculations are called operators. Go language has several types of operators as it is rich in built-in operators.
Arithmetic Operators :
Example :
CODE/PROGRAM/EXAMPLE
import "fmt" /* Program to explain Arithmetic Operators */
func main() {
var a int = 2
var b int = 1
var c int
c = a + b
fmt.Printf("Value of c is %d\n", c )
c = a - b
fmt.Printf("Value of c is %d\n", c )
c = a * b
fmt.Printf("Value of c is %d\n", c )
c = a / b
fmt.Printf("Value of c is %d\n", c )
c = a % b
fmt.Printf("Value of c is %d\n", c )
a++
fmt.Printf("Value of a is %d\n", a )
a--
fmt.Printf("Value of a is %d\n", a )
}
Output :
Value of c is 3
Value of c is 0
Value of c is 2
Value of c is 2
Value of c is 0
Value of a is 3
Value of a is 2
Logical operators :
Example :
CODE/PROGRAM/EXAMPLE
package main
import "fmt"
func main() {
var a bool = true
var b bool = false
if ( a || b ) { //Logical OR
fmt.Printf("Logical OR-Condition is true when either a or b is true\n" )
}
if ( !( b) ) { //Logical NOT
fmt.Printf("Logical NOT-Condition is true when b is false\n" )
}
b = true /*changing the value for b*/
if ( a && b ) { //Logical AND
fmt.Printf("Logical AND-Condition is true when both and b are true\n" )
}
}
Output:
Logical OR-Condition is true when either a or b is true
Logical NOT-Condition is true when b is false
Logical AND-Condition is true when both and b are true