How to concat int to string ?

5,499 views
Skip to first unread message

Jai Kumar

unread,
Sep 24, 2012, 4:25:19 PM9/24/12
to golan...@googlegroups.com

package main
import "fmt"
var pre = "prefix"
var name = ""
func main() {
sizes := []int{960,520,260}
for i := 0; i < len(sizes); i++ {
name = pre + string(sizes[i])
fmt.Println(name) //Expecting it should print prefix1 prefix2 prefix3
}
}

it prints 
prefixπ
prefixȈ
prefixĄ

but I am expecting 
prefix1 
prefix2 
prefix3

So how can I achieve my goal?

Josh Holland

unread,
Sep 24, 2012, 4:31:50 PM9/24/12
to Jai Kumar, golan...@googlegroups.com

strconv.Itoa

--
 
 

Matt Kane's Brain

unread,
Sep 24, 2012, 4:32:22 PM9/24/12
to Jai Kumar, golan...@googlegroups.com
A straight string(integer) gives you a single character string where
integer would be a valid UTF-8 codepoint.

You want fmt.Sprintf("%s%d", pre, sizes[i]) or similar. (Actually,
fmt.Sprint(pre, sizes[i]) might do it too)
> --
>
>



--
matt kane's brain
http://hydrogenproject.com

Jai Kumar

unread,
Sep 24, 2012, 5:14:49 PM9/24/12
to golan...@googlegroups.com, Jai Kumar
Perfect. Thanks Josh.
Reply all
Reply to author
Forward
0 new messages