Different Ways to Convert the Boolean Type in String in Golang
In order to convert Boolean Type to String type in Golang , you can use the strconv and fmt package function.
1. strconv.FormatBool() Method: The FormatBool is used to Boolean Type to String. It returns “true” or “false” according to the value of b.
Syntax:
func FormatBool(b bool) string
Example : Convert an Boolean Type into String using strconv.FormatBool() function.
Go
// Go program to illustrate // How to convert the // Boolean Type into String package main import ( "fmt" "strconv" ) //Main Function func main() { i := true s := strconv.FormatBool(i) fmt.Printf( "Type : %T \nValue : %v\n" , s, s) } |
Output:
Type : string Value : true
2. fmt.Sprintf() Method: The Sprintf is used to represents the string by formatting according to a format specifier.
Syntax:
func Sprintf(format string, a ...interface{}) string
Example : Convert an Boolean Type into String using fmt.Sprintf() function.
Go
// Go program to illustrate // How to convert the // Boolean Type into String package main import ( "fmt" ) //Main Function func main() { i := true s := fmt.Sprintf( "%v" , i) fmt.Printf( "Type : %T \nValue : %v\n\n" , s, s) i1 := false s1 := fmt.Sprintf( "%v" , i1) fmt.Printf( "Type : %T \nValue : %v\n" , s1, s1) } |
Output:
Type : string Value : true Type : string Value : false
Please Login to comment...