Question
i := 123
s := string(i)
In Go, s becomes a single-character string such as '{' or another Unicode character, but I want the string value "123" instead.
How can I correctly convert an int to its decimal string representation in Go?
Short Answer
By the end of this page, you will understand why string(i) does not convert an integer like 123 into the text "123" in Go. You will learn the correct way to do this using the strconv package, when to use strconv.Itoa vs strconv.FormatInt, and how integer-to-string conversion is commonly handled in real Go code.
Concept
In Go, converting a numeric value to a string depends on what kind of conversion you want.
When you write:
s := string(i)
Go does not interpret this as “turn the number into its digits.” Instead, it treats i as a Unicode code point and creates a string containing the character represented by that code point.
For example:
fmt.Println(string(65))
outputs:
A
because 65 is the Unicode code point for A.
So if i := 123, then string(i) gives the character with code point 123, which is:
{
That is why string(i) does not produce "123".
To convert an integer to its decimal text form, use the strconv package:
Mental Model
Think of an int in Go as a number in a box.
There are two different ways to interpret that box:
- As a number to display →
123should become"123" - As a character code →
123should become the character whose Unicode value is 123, which is{
So:
strconv.Itoa(123)means: write the number using digitsstring(123)means: treat 123 as a character code
A simple analogy:
strconv.Itoa(123)is like writing the label 123 on paper.string(123)is like looking up item 123 in a character table and returning the symbol found there.
Syntax and Examples
The most common way to convert an int to a string in Go is with strconv.Itoa.
package main
import (
"fmt"
"strconv"
)
func main() {
i := 123
s := strconv.Itoa(i)
fmt.Println(s)
}
Output:
123
Using strconv.FormatInt
If you are working with int64, or you want to specify the base manually, use strconv.FormatInt.
package main
import (
"fmt"
"strconv"
)
func main() {
var n int64 = 123
s := strconv.FormatInt(n, 10)
fmt.Println(s)
}
Output:
Step by Step Execution
Consider this example:
package main
import (
"fmt"
"strconv"
)
func main() {
i := 123
s := strconv.Itoa(i)
fmt.Println(s)
}
Step by step:
-
i := 123- A variable named
iis created. - Its value is the integer
123.
- A variable named
-
s := strconv.Itoa(i)- Go calls the
Itoafunction from thestrconvpackage. Itoareads the integer value123.- It converts that number into the string
"123". - The result is stored in
s.
- Go calls the
-
fmt.Println(s)- Go prints the contents of .
Real World Use Cases
Integer-to-string conversion is very common in Go programs.
Building log messages
userID := 42
msg := "processing user " + strconv.Itoa(userID)
Creating URLs or query parameters
page := 3
url := "/articles?page=" + strconv.Itoa(page)
Writing values to JSON-like text or CSV
count := 15
line := "items," + strconv.Itoa(count)
Displaying numbers in CLI tools
files := 7
fmt.Println("Processed " + strconv.Itoa(files) + " files")
Generating cache keys or map keys
id := 1001
key := "user:" + strconv.Itoa(id)
In all of these cases, the goal is to represent the number as readable text, not as a character code.
Real Codebase Usage
In real Go codebases, developers usually convert integers to strings in a few common ways depending on the context.
1. Simple conversion with strconv.Itoa
For ordinary int values:
idStr := strconv.Itoa(id)
This is the most direct and readable choice.
2. Formatting inside output with fmt.Sprintf
Sometimes developers use formatted strings when building larger messages:
msg := fmt.Sprintf("user id=%d created", id)
This is helpful when multiple values are involved.
3. Validation and early returns
A converted string may be used after checking input or constraints:
func userPath(id int) string {
if id <= 0 {
return ""
}
return "/users/" + strconv.Itoa(id)
}
This pattern combines validation with a clear conversion step.
4. Configuration and environment values
Applications often need numeric values in string form when working with environment variables, command output, headers, or URLs.
Common Mistakes
Mistake 1: Using string(i) for digit conversion
Broken code:
i := 123
s := string(i)
fmt.Println(s)
This does not print 123. It prints the character for Unicode code point 123.
Correct code:
i := 123
s := strconv.Itoa(i)
fmt.Println(s)
Mistake 2: Forgetting to import strconv
Broken code:
package main
import "fmt"
func main() {
i := 123
s := strconv.Itoa(i)
fmt.Println(s)
}
This fails because strconv was not imported.
Correct code:
import (
"fmt"
"strconv"
)
Mistake 3: Mixing and carelessly
Comparisons
| Approach | Use case | Example | Result for 123 |
|---|---|---|---|
strconv.Itoa(i) | Convert int to decimal string | strconv.Itoa(123) | "123" |
strconv.FormatInt(n, 10) | Convert int64 to decimal string | strconv.FormatInt(123, 10) | "123" |
fmt.Sprintf("%d", i) | Format values into a larger string | fmt.Sprintf("%d", 123) |
Cheat Sheet
import "strconv"
Convert int to string
s := strconv.Itoa(i)
Convert int64 to string
s := strconv.FormatInt(n, 10)
Convert uint64 to string
s := strconv.FormatUint(n, 10)
Format with fmt
s := fmt.Sprintf("%d", i)
Important rule
string(123)
means:
- convert Unicode code point
123to a string - result:
"{"
It does not mean decimal conversion.
FAQ
Why does string(123) not return "123" in Go?
Because string(123) treats 123 as a Unicode code point and converts it to the corresponding character, which is {.
What is the correct way to convert an int to a string in Go?
Use:
strconv.Itoa(i)
When should I use strconv.FormatInt instead of strconv.Itoa?
Use strconv.FormatInt when your value is int64 or when you want to choose a base such as binary, decimal, or hexadecimal.
Is fmt.Sprintf("%d", i) also valid?
Yes. It works well, especially when building larger formatted strings. For simple conversion, strconv.Itoa is usually clearer.
What does string(65) return in Go?
It returns "A" because 65 is the Unicode code point for .
Mini Project
Description
Build a small Go program that creates user profile URLs from numeric user IDs. This demonstrates the practical need to convert integers into strings when building text values such as paths, keys, and messages.
Goal
Create a program that converts integer user IDs into strings and uses them to build readable profile URLs.
Requirements
- Read or define at least three integer user IDs.
- Convert each
intID to a string correctly. - Build a URL in the form
/users/<id>for each ID. - Print each generated URL.
- Do not use
string(id)for the conversion.
Keep learning
Related questions
Check if a Value Exists in a Slice in Go
Learn how to check whether a value exists in a slice in Go, and why Go has no Python-style `in` operator for arrays or slices.
Concatenating Slices in Go with append
Learn how to concatenate two slices in Go using append and the ... operator, with examples, pitfalls, and practical usage.
Convert String to Integer in Go: Idiomatic Parsing with strconv.Atoi
Learn the idiomatic way to convert a string to an int in Go using strconv.Atoi, with examples, errors, and common mistakes.