Blog

Golang Int To String Conversion With 5 Examples

golang int to string

Golang Int to String: How to Convert an Integer to a String

Golang is a popular programming language used for web development, networking, and system administration. When working with integers in Golang, you may need to convert them to strings for various reasons. In this article, we will explore how to convert an int to a string in Golang.

Converting Int to String Using strconv.Itoa()

Golang provides the strconv package, which includes various functions to convert strings, floats, and integers to different types. To convert an integer to a string, we can use the strconv.Itoa() function. The Itoa stands for “integer to ASCII.”

Here is an example:

// declare an integer variable
var num int = 123

// convert integer to string
str := strconv.Itoa(num)

// print the result
fmt.Println(str) // output: 123

In the above example, we declared an integer variable num and assigned it a value of 123. Then, we used the strconv.Itoa() function to convert the integer to a string, and assigned the result to a variable str. Finally, we printed the result to the console using the fmt.Println() function.

Converting Int to String Using fmt.Sprintf()

We can also use the fmt.Sprintf() function to convert an integer to a string. This function works similarly to the printf() function in C programming language. It formats the given value and returns a string.

Here is an example:

// declare an integer variable
var num int = 123

// convert integer to string
str := fmt.Sprintf(“%d”, num)

// print the result
fmt.Println(str) // output: 123

In the above example, we used the fmt.Sprintf() function to format the integer value of num using the “%d” format specifier. Then, we assigned the result to a variable str and printed it to the console using the fmt.Println() function.

Converting Int to String Using strconv.FormatInt()

If you need more control over the string formatting, you can use the strconv.FormatInt() function. This function takes an integer value and a base as input and returns a string.

Here is an example:

// declare an integer variable
var num int64 = 1234567890

// convert integer to string with base 10
str := strconv.FormatInt(num, 10)

// print the result
fmt.Println(str) // output: 1234567890

In the above example, we declared an int64 variable num and assigned it a value of 1234567890. Then, we used the strconv.FormatInt() function to convert the integer to a string with base 10, and assigned the result to a variable str. Finally, we printed the string to the console using the fmt.Println() function.

Converting Int to String with Zero Padding

If you need to add zero padding to the output string, you can use the strconv.FormatInt() function with the “%0xd” format specifier, where x is the width of the output string:

var num int64 = 42
str := strconv.FormatInt(num, 10)
fmt.Println(str) // "42"
str = strconv.FormatInt(num, 10)
fmt.Println(str) // "000000042"

Converting Int to String with Comma Separation

If you want to format the output string with comma separation for thousands, you can use the “go” format specifier with the fmt.Sprintf() function:

var num int64 = 1000000
str := fmt.Sprintf("%'d", num)
fmt.Println(str) // "1,000,000"

Conclusion

Converting an int to a string in Golang is a simple process that can be done using the strconv.Itoa(), fmt.Sprintf(), or strconv.FormatInt() function. Choose the appropriate function based on your formatting requirements.

FAQs:

  • Q: How do I convert a string to an int in Golang?A: You can use the strconv.Atoi() function to convert a string to an int in Golang.
  • Q: Can I convert an int64 to a string using strconv.Itoa()?A: No, strconv.Itoa() only works with 32-bit integers. To convert an int64 to a string, use the strconv.FormatInt() function.
  • Q: How do I format an integer with leading zeros in Golang?A: You can use the strconv.Itoa() function with the “%0xd” format specifier, where x is the width of the output string.
  • Q: How do I format an integer with comma separation for thousands in Golang?A: You can use the “go” format specifier with the fmt.Sprintf() function, like this: fmt.Sprintf(“%’d”, num).
  • Q: Can I convert an int to a hexadecimal string in Golang?A: Yes, you can use the strconv.FormatInt() function with base 16.

Related posts:

How i Made $2500+ from my first venture - Crackout's Success Story
Blog

How i Made $2500+ from my first venture – Crackout’s Success Story

Let’s start it in a dramatic way. It was year 2014. Two guys were unknowingly rushing to something they could
Python string in string contains find
Blog

Check Python string in string with contains, find and % functions

Wondering how to check for a substring in with Python string in, contains, and find functions? Here’s a guide that