Go 語言程式將字串型別變數轉換為布林值
在本教程中,我們將學習如何在 Go 程式語言中將字串型別變數轉換為布林值。
布林值與字串
布林值是一種資料型別,大小為 1 位元組。它可以儲存三個值中的任何一個,主要是 True、False 或 none。它像一個標誌,用於顯示條件是否正確。
字串資料型別用於儲存一系列字元。它可以是文字或字母的形式。字串變數的大小為 1 位元組或 8 位。
對於此任務,需要各種型別的字串轉換,為了執行轉換,“strconv” 包被匯入到 Go 語言程式中。
使用 ParseBool() 方法將字串型別變數轉換為布林值
語法
func ParseBool(str string) (bool, error)
ParseBool() 函式 用於將字串轉換為整數型別值。此函式接受一個引數,即我們要轉換的字串。然後,此函式返回布林值和一個錯誤,如果轉換過程中出現任何問題,則可以在螢幕上列印該錯誤。
演算法
步驟 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() 列印了我們剛剛從字串資料型別轉換而來的布林值。
結論
我們已成功編譯並執行了 Go 語言程式程式碼,以將字串型別變數轉換為布林值。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP