Golang bool to string: FormatBool, Sprintf, and string to bool

Golang bool to string and bool to string golang: strconv.FormatBool for golang convert bool to string, golang sprintf bool with fmt.Sprintf %t or %v, golang string to bool with strconv.ParseBool, go bool to string without comparing bool to quoted true.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Golang bool to string: FormatBool, Sprintf, and string to bool

To golang bool to string (also searched as bool to string golang, golang convert bool to string, go bool to string, or boolean to string golang), use strconv.FormatBool for the clearest API, or fmt.Sprintf when you are already formatting a larger message (golang sprintf bool with the %t verb). The reverse—golang string to bool—uses strconv.ParseBool. You cannot compare a bool to the string "true" without converting one side; see string comparison in Go after both sides are strings. For format verbs in general, see string interpolation in Go; for variable types and scope.

Tested with Go 1.24 on Linux.

NOTE
%t prints booleans; %v uses the default format for any type. Prefer FormatBool when the value is only a bool.

strconv.FormatBool (preferred)

func FormatBool(b bool) string returns "true" or "false":

go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	v := true
	s := strconv.FormatBool(v)
	fmt.Printf("%T %v\n", v, v)
	fmt.Printf("%T %q\n", s, s)
}
Output

You should see bool true then string "true".


fmt.Sprintf — golang sprintf bool

Use %t for booleans; %v also works but is less explicit when the argument list mixes types:

go
package main

import "fmt"

func main() {
	b := false
	fmt.Println(fmt.Sprintf("%t", b))
	fmt.Println(fmt.Sprintf("enabled=%v", true))
}
Output

You should see false then enabled=true.


golang string to bool with ParseBool

strconv.ParseBool accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False (see docs):

go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	for _, s := range []string{"true", "0", "maybe"} {
		v, err := strconv.ParseBool(s)
		fmt.Println(s, v, err)
	}
}
Output

You should see true true <nil>, 0 false <nil>, and a non-nil error for maybe.


Do not compare bool to a string literal

This does not compile:

text
var v bool = true
if v == "true" { } // invalid operation: bool == untyped string

Convert with FormatBool / ParseBool or compare two strings after conversion.


Summary

For convert bool to string golang and go convert bool to string, strconv.FormatBool is the smallest and fastest option; fmt.Sprintf("%t", b) covers golang sprintf bool when building larger messages. For golang string to bool, use strconv.ParseBool and handle errors. That keeps logs, flags, and JSON-adjacent text consistent without illegal bool vs string comparisons.


References


Frequently Asked Questions

1. What is the best way to golang convert bool to string?

Prefer strconv.FormatBool(b); it always returns exactly "true" or "false" in English lowercase and avoids format-string mistakes.

2. How does golang sprintf bool work?

Use fmt.Sprintf with %t for boolean text or %v for the default true/false representation; strconv.FormatBool is clearer when the value is always a bool.

3. How do I golang string to bool from user input?

Use strconv.ParseBool(s) which accepts true false 0 1 t f and case-insensitive variants, and always check the error for invalid input.

4. Why does my code fail on v == "true" with a bool v?

Go does not compare bool to string directly; parse the string with ParseBool or convert the bool with FormatBool before comparing strings.

5. Is FormatBool faster than Sprintf for booleans?

Yes; FormatBool avoids the general formatter and allocates less, which matters in hot loops but rarely dominates in normal code.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …