Algorithm
-
In Go, booleans are a fundamental data type representing true or false values. Go provides relational and logical operators to work with boolean values.
Relational Operators:
-
Equality (==):
go
a == b
-
-
Checks if the values of
a
andb
are equal. -
Inequality (!=):
go
a != b
-
Checks if the values of
a
andb
are not equal. -
Greater Than (>):
go
a > b
-
Checks if the value of
a
is greater than the value ofb
. -
Less Than (<):
go
a < b
-
Checks if the value of
a
is less than the value ofb
. -
Greater Than or Equal To (>=):
go
a >= b
-
Checks if the value of
a
is greater than or equal to the value ofb
. -
Less Than or Equal To (<=):
go
a <= b
-
-
Checks if the value of
a
is less than or equal to the value ofb
.
Logical Operators:
-
Logical AND (
&&
):go
a && b
-
-
Returns true if both
a
andb
are true. -
Logical OR (
||
):go
a || b
-
Returns true if either
a
orb
is true. -
Logical NOT (
!
):go
!a
-
Returns true if
a
is false, and false ifa
is true.
Code Examples
#1 Go Boolean Data Types
Code -
Golang Programming
package main
import "fmt"
func main() {
var boolTrue bool = true
var boolFalse bool = false
fmt.Println("The boolean values are", boolTrue, "and", boolFalse)
}
Copy The Code &
Try With Live Editor
Output
#2 Relational Operators in Golang Pragramming
Code -
Golang Programming
number1 := 9
number2 := 3
result := number1 > number2
Copy The Code &
Try With Live Editor
#3 Relational Operator in Go Lang Programming
Code -
Golang Programming
// Program to illustrate the working of Relational Operators
package main
import "fmt"
func main() {
number1 := 12
number2 := 20
var result bool
// equal to operator
result = (number1 == number2)
fmt.Printf("%d == %d returns %t \n", number1, number2, result)
// not equal to operator
result = (number1 != number2)
fmt.Printf("%d != %d returns %t \n", number1, number2, result)
// greater than operator
result = (number1 > number2)
fmt.Printf("%d > %d returns %t \n", number1, number2, result)
// less than operator
result = (number1 < number2)
fmt.Printf("%d < %d returns %t \n", number1, number2, result)
}
Copy The Code &
Try With Live Editor
Output
12 != 20 returns true
12 > 20 returns false
12 < 20 returns true
#4 Logical Operator in Go Lang Programming
Code -
Golang Programming
// Program to illustrate the working of Logical Operator
package main
import "fmt"
func main() {
number1 := 6
number2 := 12
number3 := 6
var result bool
// returns false because number1 > number2 is false
result = (number1 > number2) && (number1 == number3)
fmt.Printf("Result of AND operator is %t \n", result)
// returns true because number1 == number3 is true
result = (number1 > number2) || (number1 == number3)
fmt.Printf("Result of OR operator is %t \n", result)
// returns false because number1 == number3 is true
result = !(number1 == number3);
fmt.Printf("Result of NOT operator is %t \n", result)
}
Copy The Code &
Try With Live Editor
Output
Result of OR operator is true
Result of NOT operator is false
Demonstration
Go Booleans (Relational and Logical Operators)-Go Lang Programming