How to convert int to string in Go? Looking for a working method
Hello everyone! I just started learning Go and encountered a basic but confusing task. I need to convert an integer (int) to a string so I can concatenate it with another string or print it to the screen.
In other languages (e.g., Python or Java), this is very simple: `str(42)` or `String.valueOf(42)`. But in Go, I tried this:
```go
num := 42
text := "Value: " + num
fmt.Println(text)
```
And got a compilation error: `cannot use num (type int) as type string in concatenation`. I realized that in Go you can't simply add a string and a number.
Then I tried using `string(num)` — I thought it would work like in Python:
```go
num := 42
text := "Value: " + string(num)
fmt.Println(text)
```
But the console printed something strange: `З
No answers yet.
Log in to leave an answer