How to convert a bool to a string in Go?

Casper picture Casper · Jul 24, 2016 · Viewed 64.2k times · Source

I am trying to convert a bool called isExist to a string (true or false) by using string(isExist) but it does not work. What is the idiomatic way to do this in Go?

Answer

Brrrr picture Brrrr · Jul 24, 2016

use the strconv package

docs

strconv.FormatBool(v)

func FormatBool(b bool) string FormatBool returns "true" or "false"
according to the value of b