Golang程序 将布尔值转换为字符串
在本教程中,我们将学习如何在Go编程语言中把布尔值转换为字符串。
布尔型与字符串
布尔型是一种有1个字节大小的数据类型。它可以存储三个值中的任何一个,主要是真、假或无。它就像一个标志,显示一个条件是否正确。
字符串数据类型用于存储一连串的字符,它可以是字面形式或字母形式。字符串变量的大小为1字节或8位。
例1:用strconv.FormatBool()方法将布尔类型转换为字符串。
语法
func FormatBool(b bool) string
FormatBool() 函数用于将布尔变量转换为字符串。该函数将布尔值作为参数,并返回一个字符串,我们可以很容易地将其存储在一个变量中并打印在屏幕上。这个函数存在于strconv方法中。因此,为了使用这个函数,我们必须首先将 strconv 包导入我们的程序中。
算法
- 第1步– 导入软件包 fmt 和 strconv
-
第2步 – 启动函数 main()
-
第3步 – 初始化一个布尔变量并为其赋值。
-
第4步 – 使用 strconv.FormatBool() 函数。
-
第5步 – 将结果存储在一个变量中并打印在屏幕上。
例子
// Go language program to illustrate How to convert Boolean to String
package main
// import the required packages
import (
"fmt"
"strconv"
)
// fmt package allows us to print anything.
// strconv package allows us to use other predefined functions like FormatBool()
// This is the main Function
func main() {
// Initialize a Boolean variable and assign a value to it
var i bool
i = false
// call the FormatBool() function and pass the Boolean variable as an argument in it.
// storing the results in a variable
string := strconv.FormatBool(i)
// Print the result on the screen
fmt.Printf("Succesfully converted boolean to %T and its value is %v \n", string, string)
// again assign a value to Boolean variable
i = true
// call the FormatBool() function and pass the Boolean variable as an argument to it.
// storing the results in a variable
string = strconv.FormatBool(i)
// Print the result on the screen
fmt.Printf("Succesfully converted boolean to %T and its value is %v \n", string, string)
}
输出
Succesfully converted boolean to string and its value is false
Succesfully converted boolean to string and its value is true
代码的描述
在上面的程序中,首先我们要导入 fmt 和 strconv 包。
fmt包允许我们在屏幕上打印任何东西,而 strconv 包允许我们使用其他预定义的函数,如 FormatBool( )。
现在启动 函数main( )
接下来初始化布尔类型的变量,将其转换为字符串,并给它分配一个值,如true或false。
调用 FormatBool() 函数并将布尔变量作为参数传给它。这个函数将返回字符串。将返回的字符串存储在一个变量中。
现在我们使用 fmt.Println() 函数在屏幕上打印结果。使用%T字符串格式化标志来确定输入数字的数据类型。
%T’标志是 “fmt “包的一部分。
例2:使用fmt.Sprintf()方法将布尔类型转换为字符串
在这个例子中,我们将使用fmt.Sprintf()函数将布尔类型转换为字符串。
语法
func Sprintf(format string, a ...interface{}) string
该函数返回一个格式化的字符串。它接受一些字符串格式的参数。第一个参数应该是一个字符串格式,后面是一个可变数量的参数。然后,该函数将结果作为一个格式化的字符串格式返回。
算法
- 第1步– 导入软件包 fmt 和 strconv。
-
第2步– 启动函数 main() 功能。
-
第3步 – 初始化一个 布尔 变量i并给它赋值。
-
第4步–在变量上使用 Sprintf() 函数
-
第5步– 使用%T字符串格式化标志来确定输入数字的数据类型。
-
第6步 – 在屏幕上打印出最终结果。
例子
// Go language program to illustrate How to convert Boolean to String using fmt package
package main
// import the required packages
import (
"fmt"
)
// fmt package allows us to print anything on the screen
// This is the main Function
func main() {
// initialize a variable named x of Boolean data type
var x bool
// assigning value to x variable
x = true
// use the Sprintf() function to print the formatted string
// storing the result in string variable
string := fmt.Sprintf("%v ", x)
// Print the result on the screen
fmt.Printf("Succesfully converted boolean to %T and its value is %v \n", string, string)
// repeat with other values
y := false
// use the Sprintf() function to get the formatted string
string2 := fmt.Sprintf("%v", y)
// Print the result on the screen
fmt.Printf("Succesfully converted boolean to %T and its value is %v \n", string2, string2)
}
输出
Succesfully converted boolean to string and its value is true
Succesfully converted boolean to string and its value is false
代码的描述
在上面的程序中,我们首先声明包main
我们导入了fmt 包, fmt包允许我们在屏幕上打印任何东西。
现在启动函数 main( )
接下来初始化布尔数据类型的变量,将其转换为字符串。
调用 Sprintf() 函数并将布尔变量作为参数传给它。这个函数将返回格式化的字符串。将返回的字符串存储在一个变量中。
使用%T字符串格式化标志来确定输入数字的数据类型。
%T’标志是 “fmt “包的一部分。
将结果存储到不同的变量中,并使用 fmt 包的 printf 函数在屏幕上打印结果。
总结
我们已经成功地编译并执行了Golang程序代码,使用函数将布尔值转换为字符串,并结合实例。
极客教程