How to print boolean value in Go?
As we开发者_开发百科 have %d
for int. What is the format specifier for boolean values?
If you use fmt
package, you need %t
format syntax, which will print true
or false
for boolean variables.
See package's reference for details.
%t
is the answer for you.
package main
import "fmt"
func main() {
s := true
fmt.Printf("%t", s)
}
Use %t
to format a boolean as true
or false
.
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")
}
- https://golang.org/pkg/fmt#Sprint
- https://golang.org/pkg/fmt#Sprintf
- https://golang.org/pkg/strconv#FormatBool
精彩评论