How to convert String to Boolean Data Type Conversion in Go?
Like most modern languages, Golang includes strings as a built-in type. Let's take an example, you may have a string that contains a boolean value "true". However, because this value is represented as a string, you can't perform any operation on it. You need to explicitly convert this string type into an boolean type before you can perform any operation on it.
String to Boolean Conversion
Package strconv is imported to perform conversions to and from string.ParseBool returns the boolean value represented by the string. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.
Example
package main
import (
"fmt"
"strconv"
)
func main() {
s1 := "true"
b1, _ := strconv.ParseBool(s1)
fmt.Printf("%T, %v\n", b1, b1)
s2 := "t"
b2, _ := strconv.ParseBool(s2)
fmt.Printf("%T, %v\n", b2, b2)
s3 := "0"
b3, _ := strconv.ParseBool(s3)
fmt.Printf("%T, %v\n", b3, b3)
s4 := "F"
b4, _ := strconv.ParseBool(s4)
fmt.Printf("%T, %v\n", b4, b4)
}
Output
bool, true
bool, true
bool, false
bool, false
Most Helpful This Week
How to trim leading and trailing white spaces of a string in Golang?How to import and alias package names?Example to use Weekday and YearDay functionHow to play and pause execution of goroutine?How to create a photo gallery in Go?How can we reverse a simple string in Go?Catch values from GoroutinesHow to verify a string only contains letters, numbers, underscores, and dashes in Golang?Simple function with return value in GolangRegular expression to extract numbers from a string in Golang