11. Bool Type - programming in go

Posted on Mar 04, 2019   ∣  3 min read  ∣  GO

Bool Type

It’s always good to take a look at the language specification and to become familiar and acquainted with the terminology used to talk about a programming language. That way we can be accurate and precise in understanding the language and in representing it and communicating it to others, so it’s very important to understand what the people who designed the language were intending. We will have a closer look at the Go Programming Language Specification later on.

In this section, we’re going to take a look at different types in Go. Let’s startBoolean Types

A boolean type represents the set of Boolean truth values denoted by the predeclared constants true and false. The predeclared boolean type is bool.

A Boolean is a type that can be either true or false. If we look up Boolean, it is defined as “denoting a system of algebraic notation used to represent logical propositions, especially in computing and electronics,” or “a binary variable, having two possible values called “true” and “false.”

So a Boolean is just that; true or false. It plays an important role in programming. Booleans allow us to have an expression evaluate down to a Boolean condition (true or false). And then make a decision based on that condition. So, if something is true we can do one thing, and if it’s false, we can do something else.

Later in this course, we will learn about conditional logic, switch statements, if statements, control flow, and how there is sequential control flow, and iterative control flow where you loop over something.

For now though, let’s see this Boolean value in action.

In Go, bool is a type, so let’s declare a variable x of type bool

var x bool

var x is of type bool. x holds values of type bool. We have declared the variable x, but have not assigned a value. That means if we print x, it will return its Zero Value. The Zero Value of a variable that is of type bool is false

package main

import (
	"fmt"
)

var x bool

func main() {
	fmt.Println(x)
}

playground

Some people find it strange that we don’t add a semicolon to the end of lines in Go. The semicolons are added in behind the scenes by the compiler. So, rather than x = true;, you just do x = true. Go is all about ease of programming. Remember, efficient complilation, ease of programming and efficient execution.

package main

import (
	"fmt"
)

var x bool

func main() {
	fmt.Println(x)
	x = true
	fmt.Println(x)
}

We can also do evaluations, or compare things, and get a bool in return. If we look at the operators and delimiters, we have some operators which allow us to do comparisons such as double equality ==, less than or equal to<=, greater than or equal to >=, or greater than > and less than <, not equal !=, etc.

Let’s try some out. Using the short declaration operator, let’s assign values to a couple of variables a := 7, and b := 42, then see if they are equal to each other a == b.

package main

import (
	"fmt"
)

var x bool

func main() {
	a := 7 // if we change this to 42, a == b will evaluate to true
	b := 42
	fmt.Println(a == b) // experiment with different operators: <=, >=, !=, >, <
}

playground

In Go a double equals operator == is for equality comparison, and a single equals operator = is for assignment.

The language specification for Boolean Types states

boolean type represents the set of Boolean truth values denoted by the predeclared constants true and false. The predeclared boolean type is bool.