How to convert an int value to string in Go?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting an int to a string in Go is simple, but there is one trap that catches many beginners: string(n) does not format the number in decimal text. It converts the integer to a single Unicode code point. For normal numeric formatting, the standard answer is strconv.Itoa or one of the other strconv helpers.
Use strconv.Itoa for Ordinary Decimal Conversion
Itoa stands for integer to ASCII and is the usual function for converting an int to its base-10 string form.
This prints 42, which is what most people mean by “convert an int to a string.”
Use FormatInt When You Need More Control
If you want a specific base, use strconv.FormatInt.
This is useful when the target string should be hexadecimal, binary, or another base rather than ordinary decimal.
fmt.Sprintf Works Too, But It Is Not the Leanest Tool
fmt.Sprintf can also format an integer as a string.
This is perfectly valid, especially if you are already building a larger formatted string. But for plain numeric conversion, strconv.Itoa is more direct.
Why string(n) Is Different
This is the confusion that produces wrong output:
This prints A, not 65, because 65 is interpreted as the Unicode code point for A.
So the rule is:
- use
string(n)when you want a character from a code point - use
strconv.Itoaor formatting when you want decimal digits
That distinction is fundamental in Go.
Related Cases: Unsigned Values and String Building
If the number is unsigned, use strconv.FormatUint instead of forcing it through a signed conversion path. And if you are assembling a larger message, it is often fine to combine numeric conversion with string building directly:
That keeps intent clear without pulling in formatting machinery you do not need.
Choose the Tool Based on Intent
Use strconv.Itoa when:
- the value is an
int - you want a decimal string
- you are not formatting a larger template
Use strconv.FormatInt when:
- the value is
int64 - you need base control
- the conversion is part of a serialization rule
Use fmt.Sprintf when:
- you are already composing formatted output
- readability matters more than minimizing formatting overhead
In performance-sensitive code, the strconv helpers are usually the clearer default because they express numeric conversion directly without invoking the broader formatting path.
Common Pitfalls
- Using
string(n)and expecting decimal digits. - Choosing
fmt.Sprintffor every conversion whenstrconv.Itoais simpler. - Forgetting that
FormatIntexpects anint64, not anint. - Mixing formatting intent with character-conversion intent.
Summary
- '
strconv.Itoais the normal way to convert anintto a decimal string in Go.' - '
strconv.FormatIntis useful when you need base control.' - '
fmt.Sprintfalso works, but it is a broader formatting tool.' - '
string(n)converts a code point, not a decimal number.' - Pick the conversion function that matches what the output string is supposed to mean.

