Go 语言程序将字符串类型变量转换为布尔值


在本教程中,我们将学习如何在 Go 编程语言中将字符串类型变量转换为布尔值。

布尔值与字符串

布尔值是一种数据类型,大小为 1 字节。它可以存储三个值中的任何一个,主要是 True、False 或 none。它像一个标志,用于显示条件是否正确。

字符串数据类型用于存储一系列字符。它可以是文字或字母的形式。字符串变量的大小为 1 字节或 8 位。

对于此任务,需要各种类型的字符串转换,为了执行转换,“strconv” 包被导入到 Go 语言程序中。

使用 ParseBool() 方法将字符串类型变量转换为布尔值

语法

func ParseBool(str string) (bool, error)

ParseBool() 函数 用于将字符串转换为整数类型值。此函数接受一个参数,即我们要转换的字符串。然后,此函数返回布尔值和一个错误,如果转换过程中出现任何问题,则可以在屏幕上打印该错误。

算法

  • 步骤 1 - 导入包 fmtreflectstrconv

  • 步骤 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() 打印了我们刚刚从字符串数据类型转换而来的布尔值。

结论

我们已成功编译并执行了 Go 语言程序代码,以将字符串类型变量转换为布尔值。

更新于: 2022 年 11 月 14 日

5K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.