How to convert a boolean string type value to a bool type value in Go or Golang?
Tags: go
Published: 2022-08-22 22:01:13

---
  title: How to convert a boolean string type value to a bool type value in Go or Golang?
  published: true
  tags: Go
  date: Tue Aug 23 2022 03:31:13 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/convert-boolean-string-type-value-to-bool-type-value-go-golang
  cover_image: https://melvingeorge.me/_next/static/images/main-a28140b2db3e0f43fcab193702f813ab.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/convert-boolean-string-type-value-to-bool-type-value-go-golang)
  
To convert a boolean-based `string` type value to a `bool` type value, you can use the `ParseBool()` method from the `strconv` standard package in Go or Golang.

The `ParseBool()` method:

- accepts a `string` type value as its argument
- and returns 2 values, the first one being the converted `bool` type value if any and the second being the `error` data if any.

### TL;DR

```go
package main

// import `strconv` package
import (
    "fmt"
    "strconv"
)

func main() {
    // convert `string` type value of "true"
    // using the `ParseBool()` method by passing
    // the value "true" as its argument
    convertedBoolVal, err := strconv.ParseBool("true")
    // check if any error happened
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    // log output of the `convertedBoolVal`
    // variable to the console
    fmt.Println(convertedBoolVal) // true and the type is bool ✅
}
```

For example, let's say we need to convert a boolean `string` type value of `"true"` to its corresponding `bool` type value.

To do that let's first import the `strconv` standard package like this,

```go
package main

// import `strconv` package
import "strconv"
```

After importing the package, inside the `main()` function we can use the `ParseBool()` method from the `strconv` package and pass the `string` type value of `"true"` as its argument to the function.

It can be done like this,

```go
package main

// import `strconv` package
import "strconv"

func main(){
    // convert `string` type value of "true"
    // using the `ParseBool()` method by passing
    // the value "true" as its argument
    strconv.ParseBool("true")
}
```

The `ParseBool()` method returns 2 values where the first value is the converted `bool` type value if successfully converted and the second value is the `error` data if anything happened during the conversion. To capture these 2 data, let's assign 2 variables.

It can be done like this,

```go
package main

// import `strconv` package
import "strconv"

func main(){
    // convert `string` type value of "true"
    // using the `ParseBool()` method by passing
    // the value "true" as its argument
    convertedBoolVal, err := strconv.ParseBool("true")
}
```

Before dealing with the converted `bool` value we need to check if there was any error during the conversion process. Let's use a simple `if` conditional statement and check to see if the `err` data variable is `nil` or not. If it is not `nil`, then there must have been an error that happened during the conversion.

It can be done like this,

```go
package main

// import `strconv` package
import (
    "fmt"
    "strconv"
)

func main(){
    // convert `string` type value of "true"
    // using the `ParseBool()` method by passing
    // the value "true" as its argument
    convertedBoolVal, err := strconv.ParseBool("true")
    // check if any error happened
    if err != nil {
        fmt.Println(err.Error())
        return
    }
}
```

If there is no error then it is safe to assume that the `convertedBoolVal` variable must be having the converted `bool` type value. Let's log the output to the console like this,

```go
package main

// import `strconv` package
import (
    "fmt"
    "strconv"
)

func main() {
    // convert `string` type value of "true"
    // using the `ParseBool()` method by passing
    // the value "true" as its argument
    convertedBoolVal, err := strconv.ParseBool("true")
    // check if any error happened
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    // log output of the `convertedBoolVal`
    // variable to the console
    fmt.Println(convertedBoolVal) // true and the type is bool ✅
}
```

As you can see that the `string` type value of `"true"` is successfully converted to its corresponding `bool` type value which is what we want.

See the above code live in [The Go Playground](https://go.dev/play/p/p-yHRP7DCY6).

Now let's try to give a `string` type value composed of alphabets like `John Doe` and then try to convert it to its `bool` type value like this,

```go
package main

// import `strconv` package
import (
    "fmt"
    "strconv"
)

func main() {
    // convert `string` type value of "John Doe"
    // using the `ParseBool()` method by passing
    // the value "John Doe" as its argument
    convertedBoolVal, err := strconv.ParseBool("John Doe") // Error ❌. strconv.ParseBool: parsing "John Doe": invalid syntax
    // check if any error happened
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    // log output of the `convertedBoolVal`
    // variable to the console
    fmt.Println(convertedBoolVal)
}
```

As you can see that the Go compiler shows us an error saying that `strconv.ParseBool: parsing "John Doe": invalid syntax` which essentially means that it cannot parse this value to its corresponding `bool` type value.

See the above code live in [The Go Playground](https://go.dev/play/p/RKpVrArP5Dp).

That's all 😃!

### Feel free to share if you found this useful 😃.

---