Multiple ways to Convert string to/from boolean in Golang code examples

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

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 the value - “true”|“false”, and bool type contains values - true|false.

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

This post covers the below things

  • Cast String to boolean
  • Cast boolean to String

How to Convert string to boolean in Golang?

There is an easy way to convert String to Boolean in Go Language.

strconv package provides functions - strconv.ParseBool() function which does string to bool conversion. strconv package provides many functions to do conversions from/to 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 the function signature

func ParseBool(str string) (bool, error)

This function accepts a string and returns a 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 the 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 a boolean value - false and throws Runtime error - Value: strconv.ParseBool: parsing “123”: invalid syntax
here is a 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 -

How to Convert Boolean to String in Golang

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

Convert Boolean to String golang strconv.FormatBool() function example

FormatBool() function accepts a boolean value and returns the string format of the boolean value. Here is a signature of FormatBool() method.

func FormatBool(b bool) string

This function accepts boolean values - 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

Convert boolean to string using golang fmt Sprint function example

fmt package provides string formatting functionalities. Sprint function accepts boolean or any value and returns 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
}

The output of the above program is

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

Conclusion

Performance-wise, strconv.FormatBool() function is faster than fmt.Sprint() function, So i choose strconv.FormatBool() approach to do the conversion.