Golang NOT
Go NOT Operator !
computes the logical NOT operation. NOT Operator takes a single operand and returns the result of the logical NOT operation.
The symbol used for Go NOT Operator is exclamatory mark, !
. The operand on right side of this operator symbol.
The syntax to use NOT Operator in Go language with operand x
is
!x
The above expression returns a boolean value. It returns true
if x
is false
, or false
if x
is true
.
NOT Truth Table
The truth table of NOT operator for different operand values is
x | !x |
---|---|
true | false |
false | true |
Examples
In the following example, we will take a boolean value in x
, and find its inverse using logical NOT operator.
example.go
package main
import "fmt"
func main() {
var x = true
var result = !x
fmt.Println("x :", x)
fmt.Println("!x :", result)
}
Output
x : true
!x : false
We can use NOT Operator to inverse the result of a boolean expression.
In the following example, we will check if the given number is not even. We can take a condition if the number is even, and then apply NOT operator to check if the number is not even.
example.go
package main
import "fmt"
func main() {
var x = 15
if !(x%2 == 0) {
fmt.Println("x is not even.")
} else {
fmt.Println("x is even.")
}
}
Output
x is not even.
Conclusion
In this Golang Tutorial, we learned what NOT Operator is in Go programming language, and how to use it for boolean operations, with the help of examples.