strconv.ParseBool()
functionThe ParseBool()
function returns the value in the boolean form represented by the string that is provided. It belongs to a strconv package. It only accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, and False. Values other than this will show an error.
func ParseBool(str string) (bool, error)
str
: The value of string that gets parsed to the boolean value .The returned value is a bool, error type.
The ParseBool()
function returns the boolean value represented by the string.
The following program shows how to implement the strconv.ParseBool()
function in Golang:
// Golang program// strconv.ParseBool() Functionpackage mainimport ("fmt""strconv")func main() {//function ParseBool() returns the boolean value// represented by the string.fmt.Println(strconv.ParseBool("1"))fmt.Println(strconv.ParseBool("false"))fmt.Println(strconv.ParseBool("TRUE"))fmt.Println(strconv.ParseBool("1"))fmt.Println(strconv.ParseBool("F"))// This will throw an errorfmt.Println(strconv.ParseBool("trUe"))}
Line 1: We add the main
package.
Lines 3 to 6: We import the necessary packages in Golang project.
Lines 11 to 21: We define the main()
function and the strconv.ParseBool()
function, which checks whether the string passed is a boolean value or not.