Go Programming
Data Conversion
Int to String
Coding Tutorial
GoLang Basics

How to convert an int value to string in Go?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

go
1package main
2
3import (
4    "fmt"
5    "strconv"
6)
7
8func main() {
9    n := 42
10    s := strconv.Itoa(n)
11    fmt.Println(s)
12}

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.

go
1package main
2
3import (
4    "fmt"
5    "strconv"
6)
7
8func main() {
9    n := int64(42)
10    fmt.Println(strconv.FormatInt(n, 10))
11    fmt.Println(strconv.FormatInt(n, 16))
12    fmt.Println(strconv.FormatInt(n, 2))
13}

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.

go
1package main
2
3import "fmt"
4
5func main() {
6    n := 42
7    s := fmt.Sprintf("%d", n)
8    fmt.Println(s)
9}

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:

go
1package main
2
3import "fmt"
4
5func main() {
6    n := 65
7    fmt.Println(string(n))
8}

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.Itoa or formatting when you want decimal digits

That distinction is fundamental in Go.

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:

go
count := 42
message := \"processed=\" + strconv.Itoa(count)
fmt.Println(message)

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.Sprintf for every conversion when strconv.Itoa is simpler.
  • Forgetting that FormatInt expects an int64, not an int.
  • Mixing formatting intent with character-conversion intent.

Summary

  • 'strconv.Itoa is the normal way to convert an int to a decimal string in Go.'
  • 'strconv.FormatInt is useful when you need base control.'
  • 'fmt.Sprintf also 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.