In this section, we're going to see how to convert a string value into a Boolean value:
- So, in our editor, we're going to have a variable name
isNew
, and this is going to be a string value and is a true value. We're going to use a package calledstrconv
, which hasParseBool
. It returns two things: one is the Boolean value and the other is an error. So, let's check the following code:
package main import ( "strconv" "fmt" )
func main(){ isNew := "true" isNewBool, err := strconv.ParseBool(isNew) if(err != nil){ fmt.Println("failed") }else{ if(isNewBool){ fmt.Print("IsNew") }else{ fmt.Println("Not new") } } }
- You should check whether an error is not nil. This will mean that an error occurred and we will have to handle it. For example, we're just going to output some failure message, that is,
failed
. - If it's not nil in other languages, but it's nil here, then we're going to have to check the
isNew
Boolean. If it looks...