Golang程序 将字符串类型的变量转换为布尔值
在本教程中,我们将学习如何在Go编程语言中把字符串类型的变量F转换为布尔型。
布尔型与字符串型
布尔型是一种1字节大小的数据类型。它可以存储三个值中的任何一个,主要是真、假或无。它的作用就像一个标志,显示一个条件是否正确。
字符串数据类型用于存储一连串的字符。它可以是字面形式或字母形式。字符串变量的大小为1字节或8位。
为了完成这项任务,需要进行各种类型的字符串转换,在go语言程序中导入 “strconv “包来进行转换。
使用ParseBool()方法将字符串类型的变量转换为布尔型变量。
语法
func ParseBool(str string) (bool, error)
ParseBool()函数 用于将字符串转换为整数类型的值。这个函数接受一个参数,即我们希望转换的字符串。然后,该函数返回bool值和一个错误,如果在转换过程中出现任何问题,可以打印在屏幕上。
算法
- 第1步 – 导入软件包 fmt , reflect 和 strconv
-
第2步 – 启动函数 main()。
-
第3步– 声明字符串 “str”。
-
第4步 – 使用 reflect.typeof() 函数显示变量的类型。
-
第5步– 使用 ParseBool() 方法将字符串转换为布尔值。
-
第6步 – 使用 fmt.Println() 函数打印布尔类型和布尔值。
例子
package main
//import the required packages to perform the task
import (
"fmt"
"reflect" //this package is used to display the type of variable
"strconv" //this package is used to convert the data type
)
// this is the main function
func main() {
// initialize the srting
string := "tutorialspoint"
// print the type of variable
fmt.Println("Type of the variable before conversion is: ", reflect.TypeOf(string))
// print the value of string
fmt.Println("Value of string is: ", string)
// use the ParseBool() Function on string
x, _ := strconv.ParseBool(string)
// print the type of variable
fmt.Println("Type of variable After conversion is: ", reflect.TypeOf(x))
// print the value
fmt.Println("The value of Boolean is: ", x, "\n")
// initialize the srting
string1 := "T"
// print the type of variable
fmt.Println("Type of variable Before conversion is: ", reflect.TypeOf(string1))
// print the value of string
fmt.Println("Value of string is: ", string1)
// use the ParseBool() Function on string
y, _ := strconv.ParseBool(string1)
// print the type of variable
fmt.Println("Type of variable after conversion is: ", reflect.TypeOf(y))
// print the value
fmt.Println("The value of Boolean is: ", y, "\n")
// initialize the srting
string3 := "0"
// print the type of variable
fmt.Println("Type of variable Before conversion is :", reflect.TypeOf(string3))
// print the value of string
fmt.Println("Value of string is: ", string3)
// use the ParseBool() Function on string
z, _ := strconv.ParseBool(string3)
// print the type of variable
fmt.Println("Type of variable After conversion is: ", reflect.TypeOf(z))
// print the value
fmt.Println("The value of Boolean is: ", z, "\n")
}
输出
Type of the variable before conversion is: string
Value of string is: tutorialspoint
Type of variable After conversion is: bool
The value of Boolean is: false
Type of variable Before conversion is: string
Value of string is: T
Type of variable after conversion is: bool
The value of Boolean is: true
Type of variable Before conversion is : string
Value of string is: 0
Type of variable After conversion is: bool
The value of Boolean is: false
代码的描述
-
首先,我们导入软件包fmt、reflect、strconv,其中reflect软件包用于打印变量的类型,strconv软件包用于转换数据类型。
-
然后我们启动 main() 函数来执行任务,将字符串的数据类型转换为布尔型。
-
我们将字符串初始化为一个var “string”
-
在接下来的步骤中,我们将打印我们刚刚声明的变量的类型
-
然后我们打印数据类型中的实际值。
-
然后我们从go语言的 “strconv “包中调用 ParseBool() 函数,并将字符串传递给该函数。
-
现在我们已经打印了变量的类型,以检查数据类型是否从字符串变成了布尔值。
-
最后,我们用 fmt.Printl() 打印了我们刚刚从字符串数据类型转换过来的布尔值。
总结
我们已经成功地编译并执行了Golang程序代码,将字符串类型的变量转换为布尔型变量。