Go Boolean Data Type

Go Tutorial

Go Boolean Data Type (bool) – Complete Guide with Examples

In Go (Golang), the Boolean data type is used to represent true or false values. It is mainly used in conditions, loops, comparisons, and decision-making logic.

Go keeps the boolean type simple, strict, and safe.


 What Is Boolean Data Type in Go?

The Boolean data type in Go is called bool.

It can store only two values:

  • true

  • false

Example


 

  •  Only true or false
  •  No 0 or 1 like C/C++

Declaring Boolean Variables

Using var


 

Using Short Declaration


 

  •  Clean
  • Type inferred automatically

 Default (Zero) Value of bool

If a boolean variable is declared but not initialized, its default value is false.


 

Output

false
  •  Prevents unpredictable behavior

 Boolean with Conditional Statements

Boolean values are mostly used with if, else, and switch.


 

  •  Comparison expressions return boolean values.

Booleans Comparison Operators

OperatorMeaning
==Equal
!=Not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

Example


 

 Output: true


 Logical Operators with Boolean

OperatorMeaning
&&Logical AND
`
!Logical NOT

Example


 


Boolean in Loops


 

  •  Loop runs while condition is true

Boolean as Function Return Type

Functions can return boolean values.

Usage

  •  Clean
  •  Reusable logic

Boolean with Switch Statement


 

  •  Works directly with boolean values

Boolean Formatting Output

Use %t to print boolean values.


What Go Does NOT Allow

  •  Using integers as booleans
  •  Comparing boolean with numbers
  •  Go enforces strict typing

 Common Mistakes

  •  Expecting 0 and 1 as boolean
  •  Forgetting default value is false
  •  Overusing == true
  • Complex boolean expressions

 Best Practices for Boolean in Go

  •  Use meaningful boolean names (isValid, hasAccess)
  •  Avoid == true or == false 
  •  Keep conditions readable
  •  Return booleans from helper functions
  •  Use logical operators wisely

Interview Questions: Go Boolean

1. What are the possible values of bool in Go?
true and false.

2. What is the zero value of bool?
false.

3. Can integers be used as booleans in Go?
No.

4. Which format verb prints boolean?
%t.

5. Can a function return a boolean?
Yes.


 Summary

  • bool stores true or false
  •  Default value is false
  •  Used in conditions and loops
  •  Strict typing prevents errors
  •  Essential for decision-making

Mastering the Boolean data type in Go is crucial for writing logical, safe, and professional programs

You may also like...