Go 語言提供內置支持,以通過 strconv 包實現與基本數據類型的字符串表示之間的轉換。這個包提供了一個 Itoa() 函數,它相當於 FormatInt(int64(x), 10)。或者換句話說,Itoa() 函數在基數為 10 時返回 x 的字符串表示。要訪問 Itoa() 函數,您需要在程序中借助 import 關鍵字導入 strconv 包。
用法:
func Itoa(x int) string
參數:該函數接受一個 int 類型的參數,即 x。
返回值:此函數返回一個表示 x 的字符串。
讓我們在給定示例的幫助下討論這個概念:
範例1:
// Golang program to illustrate
// strconv.Itoa() Function
package main
import (
"fmt"
"strconv"
)
func main() {
// Finding a string representation of
// the given value when the base is 10
// Using Itoa() function
fmt.Println(strconv.Itoa(23))
fmt.Println(strconv.Itoa(45))
}
輸出:
23 45
範例2:
// Golang program to illustrate
// strconv.Itoa() Function
package main
import (
"fmt"
"strconv"
)
func main() {
// Finding a string representation of
// the given value when the base is 10
// Using Itoa() function
val1:= int(24)
res1:= strconv.Itoa(val1)
fmt.Printf("Result 1:%v", res1)
fmt.Printf("\nType 1:%T", res1)
val2:= int(-21)
res2:= strconv.Itoa(val2)
fmt.Printf("\nResult 2:%v", res2)
fmt.Printf("\nType 2:%T", res2)
}
輸出:
Result 1:24 Type 1:string Result 2:-21 Type 2:string
相關用法
- Golang strconv.QuoteRune()用法及代碼示例
- Golang strconv.Quote()用法及代碼示例
- Golang strconv.QuoteRuneToGraphic()用法及代碼示例
- Golang strconv.FormatBool()用法及代碼示例
- Golang strconv.CanBackquote()用法及代碼示例
- Golang strconv.FormatUint()用法及代碼示例
- Golang strconv.IsGraphic()用法及代碼示例
- Golang strconv.IsPrint()用法及代碼示例
- Golang strconv.QuoteToGraphic()用法及代碼示例
- Golang strconv.QuoteRuneToASCII()用法及代碼示例
- Golang strconv.QuoteToASCII()用法及代碼示例
- Golang math.Lgamma()用法及代碼示例
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- Golang atomic.StoreInt64()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang bits.Sub()用法及代碼示例
- Golang io.PipeWriter.CloseWithError()用法及代碼示例
注:本文由純淨天空篩選整理自ankita_saini大神的英文原創作品 How to use strconv.Itoa() Function in Golang?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。