How to print boolean value in Go?
BooleanGoBoolean Problem Overview
As we have %d
for int. What is the format specifier for boolean values?
Boolean Solutions
Solution 1 - Boolean
If you use fmt
package, you need %t
format syntax. See package's reference for details.
Solution 2 - Boolean
Use %t
to format a boolean as true
or false
.
Solution 3 - Boolean
"%t" is the answer for you.
package main
import "fmt"
func main() {
s := true
fmt.Printf("%t", s)
}
Solution 4 - Boolean
Some other options:
package main
import "strconv"
func main() {
s := strconv.FormatBool(true)
println(s == "true")
}
package main
import "fmt"
func main() {
var s string
// example 1
s = fmt.Sprint(true)
println(s == "true")
// example 2
s = fmt.Sprintf("%v", true)
println(s == "true")
}