如何在Go中打印布尔值?

huangapple go评论77阅读模式
英文:

How to print boolean value in Go?

问题

我们可以使用%d来表示整数。布尔值的格式说明符是什么?

英文:

As we have %d for int. What is the format specifier for boolean values?

答案1

得分: 257

如果你使用fmt包,你需要使用%t格式语法,它会打印布尔变量的truefalse

有关详细信息,请参阅该包的参考文档

英文:

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.

答案2

得分: 33

%t 是你的答案。

package main
import "fmt"

func main() {
   s := true
   fmt.Printf("%t", s)
}
英文:

%t is the answer for you.

package main
import "fmt"

func main() {
   s := true
   fmt.Printf("%t", s)
}

答案3

得分: 23

使用%t来将布尔值格式化为truefalse

英文:

Use %t to format a boolean as true or false.

答案4

得分: 3

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")
}
英文:

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

huangapple
  • 本文由 发表于 2011年8月15日 05:09:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/7059735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定