Golang 将int型变量转换为字符串

Golang 将int型变量转换为字符串

在本教程中,我们将学习如何使用Golang编程语言将int型变量转换为字符串变量。

字符串被定义为一个或多个字符(字母、数字或符号)的序列。计算机应用程序经常使用字符串作为数据类型,因此在许多地方都需要将字符串转换为数字或将数字转换为字符串,特别是当我们使用用户输入的数据时。

语法

func Itoa(x int) string

Itoa() 函数是Go编程语言中的一个函数,用于获取任何整数值的字符串表示,这里用x表示。该函数接受一个整数值作为参数,并返回相应的字符串表示。

输入 − int类型

输出 − 字符串

示例:使用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的文件。我们还导入了Package strconv,实现了基本数据类型的字符串表示法之间的转换。

  • 接下来我们启动函数main(),这个函数是可执行程序的入口点。它不接受任何参数也不返回任何值。

  • 现在我们初始化一个int类型的变量并存储一个值。

  • 接下来我们调用函数Itoa()并将相应的整数值作为参数传递给它。它将int类型变量转换为string类型变量。

  • 将结果存储在一个单独的变量中,并使用fmt.Println()函数在屏幕上打印结果。

  • 为了打印结果,我们可以对比转换过程前后的变量的数据类型。

示例:使用Sprintf()函数将int类型变量转换为字符串的Golang程序:

语法

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.Println()函数在屏幕上打印result变量的数据类型以及其所包含的值。

结论

我们已成功编译并执行了Golang程序代码,将int类型变量转换为string类型变量,并提供了示例。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程