23.Conditional Logic Operators - Programming in GO

Posted on Mar 08, 2019   ∣  1 min read  ∣  GO

Conditional Logic Operators

Try to think through the following conditionals before trying them out the playgroud. Will they evaluate to true or false?

package main

import (
	"fmt"
)

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

playground

&& will return true if both sides evaluate to true, otherwise it will return false.

|| will return true if either side evaluates to true.

! returns the opposite

Try some examples for yourself.

package main

import (
	"fmt"
)

func main() {
	fmt.Printf("true && true\t %v\n", true && true)
	fmt.Printf("true && false\t %v\n", true && false)
	fmt.Printf("true || true\t %v\n", true || true)
	fmt.Printf("true || false\t %v\n", true || false)
	fmt.Printf("!true\t\t\t %v\n", !true)
}

playground