Boolean Data Type in GoLang

Boolean Data Type:

Booleans are just 'True' and 'False.' The Boolean variables contain either 'true' or 'false' in the go program. It is just like information that takes only one-bit memory from the storage in the go program. Boolean variable's value can't be converted into other types in the go language; neither will implicit conversion nor explicit conversion. It is mainly used in the conditional statements of the program.

The boolean number can also represent with the help of '0' and '1'. The developer in the go program does not use these numbers; only the compiler considers these numbers as true or false at the time of compilation. These two numeric numbers are representative of true and false, such that '0' stands for false statement, and '1' stands for truth in the conditional statement.

Boolean is defined as 'bool' data type in the go program. Boolean's values, i.e., 'true' and 'false,' will always be in the go program's lower case. It is already pre-defined in the library of the go language.

Program:

Here is a normal program of a Boolean variable in go language. The program is given below:

 package main
 // program of boolean data type
 import "fmt"
 func main() {
             var str1 = "Govind"
             var str2 = "govind"
             var str3 = "Govind"               // variable declared.
             // string comparison.
             var res1 = str1 == str2
             var res2 = str2 > str3
             var res3 = str3 == str1
             fmt.Println(res1)
             fmt.Println(res2)
             fmt.Println(res3)
             fmt.Println("The type of all three results are : ")
             fmt.Printf(" \t %T \n \t %T \n \t %T", res1, res2, res3)
 } 

Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will generate the output as below:

Boolean Data Type in GoLang

Operations on Boolean variable:

As we discussed before, Boolean variable can contain only true and false values, so only comparison and logical operation can be performed. The operators that you can use for Boolean variables are:

  1. Comparison operator: There are multiple comparison operators available in go language used on integer or float type variables, where the resultant variable will store the Boolean values. Comparison operators generate results in true or false. Comparison operators are given below:
    • Equality operator ( == )
    • Inequality operator ( != )
    • Less than operator ( < )
    • Greater than operator ( > )
    • Less than equal to ( <= )
    • Greater than equal to ( >= )

Program:

Here is a program that will illustrate the use of comparison operators for Boolean variables in the go language. The program is given below:

 package main
 // program to perform comparison operation for the boolean data type.
 import "fmt"
 func main() {
             var num1 = 10
             var num2 = 12                       // variable declaration complete.
             // use of comparison operator on the declared variable.
             var res1 = num1 == num2
             var res2 = num1 != num2
             var res3 = num1 > num2
             var res4 = num1 < num2
             var res5 = num1 <= num2
             var res6 = num1 >= num2
             // all six results will store boolean value here.
             fmt.Println(res1, res2, res3, res4, res5, res6)
 } 

Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will generate the output as below:

Boolean Data Type in GoLang
  • Logical operator: You can also use a logical operator for Boolean values. Logical operators are used in that place where there are multiple conditions available. Logical operators carry multiple comparison operators in the go program, but a minimum of two comparison operators require logical operator use. The logical operator operates on the resultant value of comparison operators. There are three types of logical operators available in the go language, which are given below:
    • Logical AND
    • Logical OR
    • Logical NOT

Program:

Here is a program that will illustrate the use of logical operators for Boolean variables in the go language. The program is given below:

 package main
 // program to perform a logical operation for a boolean data type.
 import "fmt"
 func main() {
             // use of logical operator
             var enum1 = 12<5 && 12==12
             var enum2 = 5*2==10 || 23%2==10
             // variable enum2 and enum2 stores boolean value here.
             fmt.Println(enum1, enum2)
 } 

Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will generate the output as below:

Boolean Data Type in GoLang



ADVERTISEMENT
ADVERTISEMENT