How to print boolean value in Go?

BooleanGo

Boolean 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")
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnuj VermaView Question on Stackoverflow
Solution 1 - BooleanffriendView Answer on Stackoverflow
Solution 2 - BooleanNitinView Answer on Stackoverflow
Solution 3 - BooleanUnicoView Answer on Stackoverflow
Solution 4 - BooleanZomboView Answer on Stackoverflow