Go语言教程之边写边学:类型转换-如何在Go中将布尔类型转换为字符串?
像大多数现代语言一样,Golang包含布尔值作为内置类型。让我们举个例子,你可能有一个包含布尔值true的变量。为了在Golang中将布尔值转换为字符串类型,您可以使用以下方法。
FormatBool函数
您可以使用strconv包的FormatBool()函数将布尔值转换为字符串值。FormatBool根据b的值返回“true”或“false”。
func FormatBool(b bool) stringpackage main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
var b bool = true
fmt.Println(reflect.TypeOf(b))
var s string = strconv.FormatBool(true)
fmt.Println(reflect.TypeOf(s))
}输出
bool
string系列文章
相关文章
苏公网安备
32050902101537号