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
package main
import (
"fmt"
"strconv"
)
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
package main
import (
"fmt"
)
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
Last Updated :
10 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...