Golang 将字符串类型的变量转换为布尔值
在本教程中,我们将学习如何在Go编程语言中将字符串类型的变量转换为布尔值。
布尔值与字符串
布尔值是一种具有1字节大小的数据类型。它可以存储三个主要值,即True(真)、False(假)或none(空)。它像一个标志一样,用来显示条件是否正确。
字符串数据类型用于存储字符序列。它可以是字面值或字母的形式。字符串变量的大小为1字节或8位。
为了完成此任务,需要进行各种类型的字符串转换,将“strconv”包导入到Go语言程序中。
使用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()函数来执行任务并将字符串的数据类型转换为布尔型。
-
我们将字符串初始化为变量“string”。
-
在下一步中,我们打印刚刚声明的变量的类型。
-
然后,我们打印数据类型中实际的值。
-
接下来,我们从Go语言的“strconv”包中调用ParseBool()函数,并将字符串传递给函数。
-
现在,我们已经打印出了变量的类型,以检查数据类型是否已由字符串更改为布尔型。
-
最后,我们通过使用fmt.Printl()打印出了刚刚从字符串数据类型转换的布尔值。
结论
我们已经成功编译并执行了Golang程序代码,将字符串类型变量转换为布尔型。