Golang Program to convert string type variables into Boolean


In this tutorial, we will learn how to f convert string type variables into Boolean in Go programming language.

Boolean Vs String

Boolean is a data type having 1-byte size. It can store any one of the three values mainly True, False or none. It acts like a flag to show whether a condition is correct or not.

String data type is used to store a sequence of characters. it can be in the form of literals or alphabets. The size of string variable is 1 byte or 8 bits.

For this task various types of string conversions are needed, to perform the conversions “strconv” package is imported in the go language program.

Convert string type variables into Boolean USING ParseBool() METHOD

Syntax

func ParseBool(str string) (bool, error)

ParseBool() function is used to convert strings to integer type values. This function accepts one arguments which is the string that we wish to convert. This function then returns the bool value and an error which can be printed on the screen if any problem occurs in the conversion process.

Algorithm

  • Step 1 − Import the package fmt, reflect and strconv

  • Step 2 − Start function main().

  • Step 3 − Declare the String ‘str’

  • Step 4 − Display the type of variable using reflect.typeof() function.

  • Step 5 − Convert the string to Boolean using ParseBool() Method.

  • Step 6 − Print the boolean type and the boolean value using the fmt.Println() function.

Example

package main //import the required packages to perform the task import ( "fmt" "reflect" //this package is used to display the type of variable "strconv" //this package is used to convert the data type ) // this is the main function func main() { // initialize the srting string := "tutorialspoint" // print the type of variable fmt.Println("Type of the variable before conversion is: ", reflect.TypeOf(string)) // print the value of string fmt.Println("Value of string is: ", string) // use the ParseBool() Function on string x, _ := strconv.ParseBool(string) // print the type of variable fmt.Println("Type of variable After conversion is: ", reflect.TypeOf(x)) // print the value fmt.Println("The value of Boolean is: ", x, "\n") // initialize the srting string1 := "T" // print the type of variable fmt.Println("Type of variable Before conversion is: ", reflect.TypeOf(string1)) // print the value of string fmt.Println("Value of string is: ", string1) // use the ParseBool() Function on string y, _ := strconv.ParseBool(string1) // print the type of variable fmt.Println("Type of variable after conversion is: ", reflect.TypeOf(y)) // print the value fmt.Println("The value of Boolean is: ", y, "\n") // initialize the srting string3 := "0" // print the type of variable fmt.Println("Type of variable Before conversion is :", reflect.TypeOf(string3)) // print the value of string fmt.Println("Value of string is: ", string3) // use the ParseBool() Function on string z, _ := strconv.ParseBool(string3) // print the type of variable fmt.Println("Type of variable After conversion is: ", reflect.TypeOf(z)) // print the value fmt.Println("The value of Boolean is: ", z, "\n") }

Output

Type of the variable before conversion is: string 
Value of string is: tutorialspoint 
Type of variable After conversion is: bool 
The value of Boolean is: false 

Type of variable Before conversion is: string 
Value of string is: T 
Type of variable after conversion is: bool 
The value of Boolean is: true 

Type of variable Before conversion is : string 
Value of string is: 0 
Type of variable After conversion is: bool 
The value of Boolean is: false

Description of the code

  • First, we Import the package fmt, reflect, strconv, where reflect package is used print the type of the variable and strconv package is used to convert the datatype.

  • Then we start the main() function to perform the task and convert the data type of string to Boolean.

  • We initialize the string to a var “string”

  • In the next step, we are printing the type of the variable we have just declared

  • Then we print the actual value that the data type has in it.

  • Then we are calling ParseBool() function from the “strconv” package of go language, and pass the string to the function.

  • Now we have printed the type of variable, to check if the data type is changed from string to boolean or not.

  • At last, we have printed the value of the Boolean that we have just converted from the string data type using fmt.Printl()

Conclusion

We have successfully compiled and executed the Golang program code to convert string type variables into boolean.

Updated on: 14-Nov-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements