Go Data Type Booleans

Introduction

A boolean value represents true and false.

Three logical operators are used with boolean values:

  • && which means and
  • || which means or
  • ! which means not

Here is an example program showing how they can be used:

package main/*from w  w  w  .  j a  v  a 2 s . c  om*/

import "fmt" 

func main() { 
    fmt.Println(true && true) 
    fmt.Println(true && false) 
    fmt.Println(true  || true) 
    fmt.Println(true  || false) 
    fmt.Println(!true) 
} 

We usually use truth tables to define how these operators work:

Expression Value
true && truetrue
true && false false
false && true false
false && false false
true || truetrue
true || false true
false || true true
false || false false
!true false
!false true



PreviousNext

Related