Golang Examples - Convert string to boolean or boolean to string datatype


In this blog post, You will learn how String conversion from Boolean type in go language with examples.

Golang Examples - Convert string to boolean or boolean to string datatype

Golang String bool conversion

string and bool are predefined data types available in Golang. String accepts a group of characters, bool accepts true or false values. for example, string type contains value - “true"|"false”, bool type contains values - true|false.

In Go language, String conversion will not happen automatically. We have to write a custom manual lcode or use standard packages - fmt or strconv to do that. 

This post covers the below things

  • Cast String to boolean
  • Cast boolean to String

Convert string to boolean examples

There is an easy way to convert String to Boolean in Go Language. strconv package provides functions -  strconv.ParseBool() function which do string to bool conversion. strconv package provides many functions to do conversions from to and from string values. ParseBool() function takes boolean value in the form String(“true”) and returns a boolean value.

For Example, if you send “true”, It returns the equivalent boolean primitive value true. if you send “false”, return boolean false.

Here is function signature

func ParseBool(str string) (bool, error) 

This function accepts string and return bool value type and error.

if string contains “1”, “t”, “T”, “true”, “TRUE”, “True” . It return boolean value - true and type is bool, error - nil
if string contains “", “f”, “F”, “false”, “FALSE”, “False”, It returns boolean value - false, error - nil

if a string contains other than two above two types of values, It returns boolean value - false and throws Runtime error - Value: strconv.ParseBool: parsing “123”: invalid syntax
here is an strconv.ParseBool() function example

package main  
  
import (  
 "fmt"  
 "strconv"  
)  
  
func convertStringToBool(str string) {  
 boolValue, err := strconv.ParseBool(str)  
 if err == nil {  
  fmt.Printf("DataType: %T  \n", boolValue) // Display DataType  
  fmt.Println("value - ", boolValue)        // variable value  
 }  
 fmt.Println("error - ", err)  
}  
func main() {  
 convertStringToBool("True")  
 convertStringToBool("T1rue")  
 convertStringToBool("0")  
}  

when the above program is compiled and executed, It gives the following the output

DataType: bool  
value -  true  
error -  error -  strconv.ParseBool: parsing "T1rue": invalid syntax  
DataType: bool  
value -  false  
error - 

Convert Boolean to String 

There are two ways to convert boolean to String in Go language. one is using strconv.FormatBool(bool) function and other is using fmt.sprint() function

strconv.FormatBool() function example 

FormatBool() function accepts boolean value and return string format of boolean value Here is an signature of FormatBool() method.

func FormatBool(b bool) string 

This function accepts boolean value - true or false. It returns the string “true”, “false”.
Here is an example program

func main() {  
  
 convertBooleanToString(true)  
 convertBooleanToString(false)  
  
}  
  
func convertBooleanToString(boolValue bool) {  
 strValue := strconv.FormatBool(boolValue)  
 fmt.Printf("DataType: %T  \n", strValue) // Display DataType  
 fmt.Println("value - ", strValue)        // variable value  
}  

When the above program is compiled, the output of the above program is

DataType: string  
value -  true  
DataType: string  
value -  false  

fmt Sprint function example

fmt package provides string formatting functionalities. Sprint function accepts boolean or any value and return a string.

here is an example of conversion

func main() {  
 convertBooleanToString(true)  
 convertBooleanToString(false)  
  
}  
func convertBooleanToString(boolValue bool) {  
 strValue := fmt.Sprint(boolValue)  
 fmt.Printf("DataType: %T  \n", strValue) // Display DataType  
 fmt.Println("value - ", strValue)        // variable value  
}  

Output of the above program is

DataType: string  
value -  true  
DataType: string  
value -  false  

Performance wise, strconv.FormatBool() function is faster than fmt.Sprint() function.

Please Like and share this post if you like it.

Similar Posts