How to convert int to string in Go: a complete guide with examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

"I racked my brain for an hour, and the solution is three lines": how to convert int to string in Go

\n\n

Imagine: you're writing a Go microservice, processing numeric user IDs, and suddenly realize you need to concatenate them with text in logs or an HTTP response. You write something like str := "User_" + id — and the compiler throws an error: cannot use id (type int) as type string in concatenation. Sound familiar?

\n\n

This is one of the most popular questions on Stack Overflow about Go — "How to convert an int value to string in Go?" — it has been viewed hundreds of thousands of times. And for good reason: Go has strong typing, and you simply can't "add" a number to a string. But don't worry — today we'll cover all the ways to convert int to string in Go. You'll learn which method is faster, which is safer, and when to use what. Let's go!

\n\n

Why doesn't Go allow simply adding int and string?

\n\n

Unlike JavaScript or Python, where "Age: " + 25 works "out of the box," Go requires explicit type conversion. This is done for safety: you must always control what is being converted into what. Imagine accidentally adding a number to text — in other languages, this can lead to hard-to-find bugs. In Go, the compiler will immediately say: "Stop, you can't do that."

\n\n

Therefore, to convert int to string in Go, there are special functions from the standard library. No external dependencies — just pure Go.

\n\n

Method #1: fmt.Sprintf — the universal "Swiss Army knife"

\n\n

The most intuitive way is to use the fmt.Sprintf function. You're probably already familiar with it from formatted output. It can convert anything into a string — numbers, boolean values, structs.

\n\n
package main\n\nimport "fmt"\n\nfunc main() {\n    age := 25\n    str := fmt.Sprintf("Age: %d", age)\n    fmt.Println(str) // Output: Age: 25\n}
\n\n

Pros:

\n
    \n
  • Very flexible: you can immediately embed a number into a string with the desired format (decimal, hexadecimal, with leading zeros).
  • \n
  • Suitable for any type — not only int, but also float, bool, struct.
  • \n
\n\n

Cons:

\n
    \n
  • Uses reflection under the hood, so it works slower than specialized functions.
  • \n
  • If you just need to get a string from a number (without formatting), it's overkill.
  • \n
\n\n

When to use: when you need not just to convert, but also to format the output — add currency symbols, align by width, convert to HEX.

\n\n

Method #2: strconv.Itoa — fast and simple (int → string)

\n\n

If your task is simply to get the string representation of a number, without any "embellishments," use strconv.Itoa. This is a specialized function for converting int to string. It works quickly and doesn't perform unnecessary operations.

\n\n
package main\n\nimport (\n    "fmt"\n    "strconv"\n)\n\nfunc main() {\n    number := 42\n    str := strconv.Itoa(number)\n    fmt.Println(str) // Output: "42"\n    fmt.Printf("Type: %T\\n", str) // Type: string\n}
\n\n

Pros:

\n
    \n
  • Maximum performance for the basic case.
  • \n
  • Simplicity: one function — one action.
  • \n
\n\n

Cons:

\n
    \n
  • Only works with the int type. If you have int64, int32, etc., you need to first cast to int or use another method.
  • \n
  • No control over the number base — only decimal representation.
  • \n
\n\n

When to use: when you have a regular int and just need to get a string — this is your default choice.

\n\n

Method #3: strconv.FormatInt — control over the number base

\n\n

But what if you need to convert a number to

Blogs

Book Recommendations