Golang OR

Go OR Operator || computes the logical OR operation. OR Operator takes two operands and returns the result of their logical OR operation.

The symbol used for Go OR Operator is double vertical bar, ||. The operands go on either side of this operator symbol.

The syntax to use OR Operator in Go language with operands x and y is

x || y

The above expression returns a boolean value. It returns true if at least one of the operands is true, or false otherwise.

OR Truth Table

The truth table of OR operator for different operand values is

xyx || y
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Examples

In the following example, we will take boolean values in x and y, and find the result of their logical OR 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 : true

We can also combine one or more simple boolean conditions, using OR operator, to form a compound condition.

In the following example, we will check if the given number is even or divisible by 5.

example.go

package main

import "fmt"

func main() {
	var x = 15
	if x%2 == 0 || x%5 == 0 {
		fmt.Println("x is even or divisible by 5 or both.")
	} else {
		fmt.Println("x is neither even nor divisible by 5.")
	}
}

Output

x is even or divisible by 5 or both.

Conclusion

In this Golang Tutorial, we learned what OR Operator is in Go programming language, and how to use it for boolean operations, with the help of examples.