Gaaker Go

Golang Logical operations

Logical operators include !, &&, ||, ==, !=, <, <=, >, >=.

They are called logical operators because their results are always boolean.

Not operator (!)
The non-operator is used to get the opposite result of Boolean.
And operator (&&)
Compare two expressions or values, and return true only when the compared values are true.
Or operator (||)
Compare two expressions or values, and return true if only one of the two parties is true.
Equality operator (==)
Compare whether the two sides are equal, and return true if they are equal, otherwise return false.
Inequality operator (!=)
Compare whether the two sides are not equal, return true if they are not equal, otherwise return false.

The following is a complete code example.

package main

import "fmt"

func main() {
    fmt.Println("!true -> ", !true)

    fmt.Println("true && false -> ", true && false)
    fmt.Println("true || false -> ", true || false)

    fmt.Println("1 > 2 -> ", 1 > 2)

    fmt.Println("1==1", 1 == 1)
}

Below is the program output:

!true ->  false
true && false ->  false
true || false ->  true
1 > 2 ->  false
1 == 1 ->  true
< Logical operations Logical operations >