how to convert bool to string in golang?
August 14, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
To convert bool to string in golang, use strconv.FormatBool(v)
it will convert the boolean value to string. You have only to pass your boolean value in the FormatBool()
method.
Follow the below tutorial if you are struggling with installing GO in windows.
https://aguidehub.com/blog/how-to-install-golang-in-windows/
Today, I will show you how do I convert bool to string in golang, as mentioned above I’m going to use the strconv.FormatBool(v)
method.
To use the strconv.FormatBool(v)
method, we have first import "strconv"
package which is built in provided by golang, then we have to pass our boolean within the strconv.FormatBool(v)
method.
Let’s start our Golang convert bool to string example
main.go
package main
import (
"fmt"
"strconv"
)
func main() {
isExist := true
str := strconv.FormatBool(isExist)
fmt.Println(str) //true
fmt.Printf("%q\n", str) //"true"
}
In the above example, we have converted a bool to a string and printed it in the console. let’s check the output.
Output
true
"true"
I hope it helps you, All the best 👍.