當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang strconv.FormatBool()用法及代碼示例

Go 語言提供內置支持,以通過 strconv 包實現與基本數據類型的字符串表示之間的轉換。這個包提供了一個 FormatBool() 函數,用於根據 x 的值返回真或假。要訪問 FormatBool() 函數,您需要借助 import 關鍵字在程序中導入 strconv 包。

用法:

func FormatBool(x bool) string

參數:該函數接受一個 bool 類型的參數,即 x。

返回值:該函數根據 x 的值返回真或假。

讓我們在給定示例的幫助下討論這個概念:



範例1:


// Golang program to illustrate
// strconv.FormatBool() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
  
    // Finding true or false
    // according to the input value
    // Using FormatBool() function
    fmt.Println(strconv.FormatBool(true))
    fmt.Println(strconv.FormatBool(false))
  
}

輸出:

true
false

範例2:


// Golang program to illustrate
// strconv.FormatBool() Function
  
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
  
    // Finding true or false
    // according to the input value
    // Using FormatBool() function
    val1:= true
    res1:= strconv.FormatBool(val1)
    fmt.Printf("Result 1:%v", res1)
    fmt.Printf("\nType 1:%T", res1)
  
    val2:= false
    res2:= strconv.FormatBool(val2)
    fmt.Printf("\nResult 2:%v", res2)
    fmt.Printf("\nType 2:%T", res2)
  
}

輸出:

Result 1:true
Type 1:string
Result 2:false
Type 2:string



相關用法


注:本文由純淨天空篩選整理自ankita_saini大神的英文原創作品 How to use strconv.FormatBool() Function in Golang?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。