Learning Golang: Logical operators

Share on:

This is part 20 of my journey learning Golang.

Boolean algebra

Boolean algebra is about logical operations applied to the boolean values true and false.

The most common operations are “and” (conjunction), “or” (disjunction) and “not” (negation).

Fun fact: it is called “boolean” because it was introduced by George Boole in 1847.

Logical operators

Go provides three operators for boolean algebra, called “logical operators”:

  • &&: AND operator.
  • ||: OR operator.
  • !: NOT operator.

And

The && operator takes two boolean values and it produces true if and only if both its operands are true.

In other words: p && q is “if p then q else false”.

In table format:

true false
true true false
false false false

Example:

 1package main
 2
 3import (
 4  "os"
 5  "bufio"
 6  "fmt"
 7  "strings"
 8)
 9
10func main() {
11  reader := bufio.NewReader(os.Stdin)
12
13  fmt.Print("Are the lights on (y/n)? ")
14  lightsOnInput, _ := reader.ReadString('\n')
15  lightsOnCleaned := strings.TrimSuffix(lightsOnInput, "\n")
16  lightsOn := lightsOnCleaned == "y"
17
18  fmt.Print("Is the door open (y/n)? ")
19  doorOpenInput, _ := reader.ReadString('\n')
20  doorOpenCleaned := strings.TrimSuffix(doorOpenInput, "\n")
21  doorOpen := doorOpenCleaned == "y"
22
23  if lightsOn && doorOpen {
24    fmt.Println("Go in.")
25  } else {
26    fmt.Println("Come back later.")
27  }
28}

Source: learning-go/logical/and.go

Sample runs:

 1❯ bin/go run ./logical/and.go
 2Are the lights on (y/n)? y
 3Is the door open (y/n)? y
 4Go in.
 5
 6❯ bin/go run ./logical/and.go
 7Are the lights on (y/n)? n
 8Is the door open (y/n)? y
 9Come back later.
10
11❯ bin/go run ./logical/and.go
12Are the lights on (y/n)? y
13Is the door open (y/n)? n
14Come back later.

Or

The || operator takes two boolean values and it produces true if either of its operands is true (or if both are true).

In other words: p || q is “if p then true else q”.

In table format:

true false
true true true
false true false

Example:

 1package main
 2
 3import (
 4  "bufio"
 5  "fmt"
 6  "os"
 7  "strings"
 8)
 9
10func main() {
11  reader := bufio.NewReader(os.Stdin)
12
13  fmt.Print("What day of the week is it? ")
14  dowInput, _ := reader.ReadString('\n')
15  dow := strings.TrimSuffix(dowInput, "\n")
16
17  if dow == "Saturday" || dow == "Sunday" {
18    fmt.Println("Stay home.")
19  } else {
20    fmt.Println("Go to work.")
21  }
22}

Source: learning-go/logical/or.go

Sample runs:

 1❯ bin/go run ./logical/or.go
 2What day of the week is it? Saturday
 3Stay home.
 4
 5❯ bin/go run ./logical/or.go
 6What day of the week is it? Sunday
 7Stay home.
 8
 9❯ bin/go run ./logical/or.go
10What day of the week is it? Monday
11Go to work.

Not

The ! operator returns true if its operand is false and false if its operand is true. It reverses (“negates”) the value of its operand.

In other words: !p is “not p”.

In table format:

!
true false
false true

Example:

 1package main
 2
 3import (
 4  "bufio"
 5  "fmt"
 6  "os"
 7  "strings"
 8)
 9
10func main() {
11  reader := bufio.NewReader(os.Stdin)
12
13  fmt.Print("Are you hungry (y/n)? ")
14  hungryInput, _ := reader.ReadString('\n')
15  hungryCleaned := strings.TrimSuffix(hungryInput, "\n")
16  hungry := hungryCleaned == "y"
17  notHungry := !hungry
18
19  fmt.Println("You are hungry:", hungry)
20  fmt.Println("You are satisfied:", notHungry)
21}

Source: learning-go/logical/not.go

Sample runs:

1❯ bin/go run ./logical/not.go
2Are you hungry (y/n)? y
3You are hungry: true
4You are satisfied: false
5
6❯ bin/go run ./logical/not.go
7Are you hungry (y/n)? n
8You are hungry: false
9You are satisfied: true

Takeaways

  • Go has three operators for boolean arithmetic: && (and), || (or) and ! (not).
  • These operators are very useful for writing conditions for if statements.
  • They’re also useful whenever a boolean value needs to be produced based on one or more boolean values.
  • Relational operators (such as ==, <, >, etc.) return boolean values and are easy to use in boolean expressions.

comments powered by Disqus