Go 語言程式:將布林值轉換為字串


在本教程中,我們將學習如何在 Go 程式語言中將布林值轉換為字串。

布林值與字串

布林值是一種資料型別,大小為 1 位元組。它可以儲存三個值中的任何一個,主要是 True、False 或 none。它就像一個標誌,用於顯示某個條件是否正確。

字串資料型別用於儲存字元序列。它可以是文字或字母的形式。字串變數的大小為 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

程式碼描述

在上面的程式中,我們首先需要匯入fmtstrconv 包。

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 - 匯入fmtstrconv 包。

  • 步驟 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 函式在螢幕上列印結果。

結論

我們已成功編譯並執行了 Go 語言程式程式碼,使用函式將布林值轉換為字串,並附帶了示例。

更新於: 2022-11-14

4K+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告