Golang AND
Go AND Operator &&
computes the logical AND operation. AND Operator takes two operands and returns the result of their logical AND operation.
The symbol used for Go AND Operator is double ampersand, &&
. The operands go on either side of this operator symbol.
The syntax to use AND Operator in Go language with operands x
and y
is
x && y
The above expression returns a boolean value. It returns true
if both the operands are true
, or false
otherwise.
AND Truth Table
The truth table of AND operator for different operand values is
x | y | x && y |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Examples
In the following example, we will take boolean values in x
and y
, and find the result of their logical AND operation.
example.go
package main
import "fmt"
func main() {
var x = true
var y = false
var result = x && y
fmt.Println("x :", x)
fmt.Println("y :", y)
fmt.Println("x && y :", result)
}
Output
x : true
y : false
x && y : false
We can also combine one or more simple boolean conditions, using AND operator, to form a compound condition.
In the following example, we will check if the given number is even and divisible by 5
.
example.go
package main
import "fmt"
func main() {
var x = 20
if x%2 == 0 && x%5 == 0 {
fmt.Println("x is even and divisible by 5.")
} else {
fmt.Println("x is not even or not divisible by 5 or both.")
}
}
Output
x is even and divisible by 5.
Conclusion
In this Golang Tutorial, we learned what AND Operator is in Go programming language, and how to use it for boolean operations, with the help of examples.