How do you convert an int to a string in Golang, and what’s the correct way to concatenate strings?

Hello there!!

I’m working with Go and have a couple of questions about fundamental type conversions and string manipulations.

First, I’m trying to convert an int to a string, like this:

i := 123
s := string(i)`

However, instead of getting "123", I’m getting a strange character ('E'). This indicates I’m not performing a proper golang int to string conversion.

Second, in Java, I can easily concatenate strings like this:

String s = "ab" + "c"; // s is "abc"

I’m looking for the idiomatic way to achieve this type of string concatenation in Go.

Any insights on the correct and proper methods for both of these operations in Go would be greatly appreciated.

Yep, I ran into the same thing when I was new to Go. Doing string(i) doesn’t convert the integer to its textual representation; it actually gives you the character for that Unicode code point.

So string(123) becomes ‘{’ or similar, not “123”.

The right way to convert an int to a string in Golang is:

import "strconv"

i := 123
s := strconv.Itoa(i)

strconv.Itoa() stands for “Integer to ASCII.” That’s the cleanest method I use across my projects.

You’re absolutely right to question this. Go is strict and doesn’t let you get away with shortcuts like some other languages.

As others have said, string(i) interprets the number as a byte, not a string value.

Also, for string concatenation in Go, it’s pretty straightforward:

s1 := "ab"
s2 := "c"
result := s1 + s2 // result is "abc"

I use this pattern all the time in log formatting or building messages.

And if you’re dealing with performance-critical code, strings.Builder is a more efficient way for many string operations.

I had the same confusion coming from a Java background! It took me a while to understand why “string(i)” wasn’t giving me the numeric value as a string.

For int-to-string in Go: s := strconv.Itoa(i)

If you ever need to parse back from string to int, you can use: i, err := strconv.Atoi(s)

Also, like in Java, string concatenation is just using +, but Go doesn’t allow overloading, so types need to match. Always make sure both operands are strings, otherwise, the compiler will yell at you :sweat_smile: