how to convert bool to string in golang?

Hi 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 👍.

Try it Yourself

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts