Go語言程式:將int型別變數轉換為字串


在本教程中,我們將學習如何使用 Go 程式語言將 int 型別變數轉換為字串變數。

字串被定義為一個或多個字元(字母、數字或符號)的序列。計算機應用程式經常使用字串作為資料型別,因此,在許多地方需要將字串轉換為數字或數字轉換為字串,尤其是在使用使用者輸入的資料時。

語法

func Itoa(x int) string

Go 程式語言中的 **Itoa()** 函式用於獲取任何整數值的字串表示形式,這裡用 x 表示。此函式以整數值作為引數,並返回該數字對應的字串表示形式。

**輸入** - int 型別

**輸出** - 字串

示例:使用 iota() 函式將 int 型別變數轉換為字串的 Go 語言程式

使用 Go 標準庫中 strconv 包的 strconv.Itoa() 方法,我們可以將數字轉換為字串。如果將數值作為數字或變數傳遞到方法的括號中,它將被更改為字串值。

演算法

  • **步驟 1** - 匯入 fmt 和 strconv 包。

  • **步驟 2** - 啟動 main() 函式

  • **步驟 3** - 初始化整型變數併為其分配適當的值。

  • **步驟 4** - 列印變數的資料型別

  • **步驟 5** - 使用 strconv.Itoa() 函式將整型值轉換為字串。

  • **步驟 6** - 將結果儲存在單獨的變數中

  • **步驟 7** - 使用 fmt.Println() 函式在螢幕上列印結果

示例

// Go program to illustrate How to convert int to String using Ita() function. package main // fmt package provides the function to print anything // strconv Package allows us to use other predefined functions like Iota(). import ( "fmt" "strconv" ) // start function main() func main() { // declare and initialize a int type variable and assign value to it number := 20 fmt.Printf("Type of variable before conversion: %T \nValue : %v\n", number, number) //use the strconv method on the the variable to convert it to string and store the result in a seperate variable. result := strconv.Itoa(number) // print the result on the screen using fmt.Println() function // check the data type of variable after conversion process. fmt.Printf("Type of variable after conversion : %T \nValue : %v\n", result, result) }

輸出

Type of variable before conversion: int
Value : 20
Type of variable after conversion : string
Value : 20

數字 12 周圍的引號表示它現在是字串值而不是整數。

程式碼描述

  • 在上面的程式中,我們首先宣告包 main。

  • 我們匯入了包含 fmt 包檔案的 fmt 包。我們還匯入了 Package strconv,它實現了對基本資料型別字串表示形式的轉換。

  • 接下來我們啟動 main() 函式,此函式是可執行程式的入口點。它不接受任何引數也不返回任何內容。

  • 現在我們初始化一個 int 型別變數並存儲一個值。

  • 接下來,我們呼叫 Itoa() 函式並將相應的整數值作為引數傳遞給它。它將 int 型別變數轉換為字串型別變數。

  • 將結果儲存在單獨的變數中,並使用 fmt.Println() 函式在螢幕上列印結果。

  • 要列印結果,我們可以對比轉換過程前後變數的資料型別。

示例:使用 Sprintf() 函式將 int 型別變數轉換為字串的 Go 語言程式

語法

func Sprintf(format string, a ...interface{}) string

此函式返回格式化的字串。第一個引數應該是字串格式,後面跟著可變數量的引數。然後此函式將結果作為格式化的字串格式返回。

演算法

  • **步驟 1** - 匯入 fmt 和 strconv 包。

  • **步驟 2** - 啟動 main() 函式

  • **步驟 3** - 初始化整型變數併為其賦值。

  • **步驟 4** - 列印變數的型別。

  • **步驟 5** - 使用 Sprintf() 函式將整型值轉換為字串。

  • **步驟 6** - 轉換過程後再次列印變數的型別。

示例

// Go program to illustrate How to convert int to String using Sprintf() function. package main // fmt package provides the function to print anything import ( "fmt" "reflect" ) // start function main() func main() { // declare and initialize a int type variable and assign value to it number := 200 fmt.Println("Number =",number) var num = reflect.TypeOf(number) fmt.Println("Type of variable before conversion =",num) // use the strconv method on the the variable to convert it to string and store the result in a // seperate variable. result := fmt.Sprintf("%s", number) var res = reflect.TypeOf(result) fmt.Println("Type of Variable = ",res) fmt.Println("Value =",result) }

輸出

Number = 200
Type of variable before conversion = int
Type of Variable = string
Value = %!s(int=200)

程式碼描述

  • 首先宣告包 main。

  • 匯入 fmt 包,允許我們在螢幕上列印任何內容。

  • 現在呼叫 main() 函式,這是可執行程式的入口點。此函式不返回任何內容也不接受任何引數。

  • 現在初始化一個名為 number 的整型變數併為其分配適當的值。

  • 現在使用 Sprintf() 函式將整型值轉換為字串,並將整數 number 作為引數傳遞給它。

  • 將輸出儲存在單獨的變數中,這裡我們將其命名為 result。

  • 使用 fmt.Ptintln() 函式在螢幕上列印 result 變數的資料型別及其所具有的值。

結論

我們已成功編譯並執行了 Go 語言程式程式碼,用於將 int 型別變數轉換為字串型別變數,並附帶了示例。

更新於: 2022-11-22

16K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.