在本教程中,我们将学习如何使用Golang编程语言将int类型变量转换为字符串变量。
字符串被定义为一个或多个字符(字母、数字或符号)的序列。计算机应用程序经常使用字符串作为数据类型,因此,有必要在许多地方将字符串转换为数字或数字转换为字符串,特别是当我们使用用户输入的数据时。
语法
func Itoa(x int) string
Go编程语言中的 Itoa() 函数用于获取任何整数值的字符串表示,这里用x来描述。这个函数以一个整数值为参数,并返回该数字的相应字符串表示。
INPUT - int类型
OUTPUT - 字符串
例子:使用iota()函数将int类型的变量转换为字符串的Golang程序
使用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的文件。我们还导入了包strconv,实现了基本数据类型的字符串表示法之间的转换。
-
接下来我们启动函数main(),这个函数是可执行程序的入口点。它不接受任何参数,也不返回任何东西。
-
现在我们初始化一个int类型的变量并存储一个值。
-
接下来我们调用函数Itoa()并将相应的整数值作为参数传给它。它将int类型的变量转换为字符串类型的变量。
-
将结果存储在一个单独的变量中,并使用fmt.Println()函数在屏幕上打印结果。
-
为了打印结果,我们可以对比转换过程前后变量的数据类型。
例子:Golang程序使用Sprintf()函数将int类型的变量转换为字符串。
语法
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()函数将整数类型的值转换成字符串,并将整数作为参数传给它。
-
将输出存储在一个单独的变量中,这里我们将其命名为结果。
-
使用fmt.Ptintln()函数在屏幕上打印结果变量的数据类型和它所拥有的值。
总结
我们已经成功地编译并执行了Golang程序代码,将int类型的变量转换为字符串类型的变量,并举例说明。