Go Programming Language Logical Operators

Go Programming Language Logical Operators

Logical operators are used to determine logical operations between variables, values or expressions.

Operator Description
&& and
|| or
! not

Go Program Example for Logical Operators

Go code is used to illustrate the logical operations &&, || and !

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
                                                                           	 
	// declare variables                                                   	 
	var a, b int                                                           	 
                                                                           	 
	// assign variables                                                    	 
	a = 10                                                                 	 
	b = 6                                                                  	 
                                                                           	 
	fmt.Println("Logical Operators &&, ||, !")                             	 
	// and operation                                                       	 
	fmt.Printf("(%d > %d && %d != %d): %t\n", a, b, a, b, (a>b && a!=b))   	 
	// or operation                                                        	 
	fmt.Printf("(%d == %d || %d>%d): %t\n", a, b, a, b, (a==b || a>b))     	 
	// not operation                                                       	 
	fmt.Printf("!(%d <= %d): %t\n", a, b, !(a<=b))                         	 
}
Output:
$ go build logical-operators.go
$ ./logical-operators
Logical Operators &&, ||, !
(10 > 6 && 10 != 6): true
(10 == 6 || 10>6): true
!(10 <= 6): true