i := 123
s := string(i) 

s is 'E', but what I want is "123"

Please tell me how can I get "123".

And in Java, I can do in this way:

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

how can I concat two strings in Go?

up vote 460 down vote accepted

Use the strconv package's Itoa function.

For example:

package main

import (
    "strconv"
    "fmt"
)

func main() {
    t := strconv.Itoa(123)
    fmt.Println(t)
}

You can concat strings simply by +'ing them, or by using the Join function of the strings package.

  • 4
    If you have a lot of string concatenations to do (for example to build a long string) and want your code to be faster and with less allocations, you should consider using bytes.Buffer too (it's a kind of more generic version of Java's StringBuilder). – Denys Séguret Apr 11 '12 at 15:36
  • 15
    Why did the language designers think that cryptic functions names like "Itoa" were preferable to something that might be a little more descriptive? – Luke Jul 17 '16 at 20:27
  • 5
    @luke it comes from the C heritage where the entire machine might have 256K memory; usability was sacrificed to fit more functionality in. The creators of Go are all deeply embedded in that heritage and feel entirely comfortable with these names. – Bryan Sep 29 '16 at 10:15
  • 19
    Putting history above accessibility and ease of learning is bad design IMO. :( – Luke Sep 29 '16 at 14:00
  • 7
    @Luke Depends on who your target users are and how strong the convention is. Some UIs still have a floppy disk as the Save icon :) – Nathron Nov 14 '16 at 23:43
fmt.Sprintf("%v",value);

If you know the specific type of value use the corresponding formatter for example %d for int

More info - fmt

  • 7
    I like this option with out to add libraries. Thanks!!! – didando8a Jul 4 '15 at 17:40
  • 2
    fmt.Sprint(value) works too – stacker-baka Apr 16 at 11:37
  • %d for int - this – scniro Jun 4 at 20:41

You can use fmt.Sprintf

See http://play.golang.org/p/bXb1vjYbyc for example.

It is interesting to note that strconv.Itoa is shorthand for

func FormatInt(i int64, base int) string

with base 10

For Example:

strconv.Itoa(123)

is equivalent to

strconv.FormatInt(int64(123), 10)
  • undefined: strconv in strconv.Itoa – Felo Vilches Jun 30 '17 at 5:10
  • @FeloVilches import "strconv" – kgthegreat Aug 14 '17 at 11:29

fmt.Sprintf, strconv.Itoa and strconv.FormatInt will do the job. But Sprintf will use the package reflect, and it will allocate one more object, so it's not a good choice.

enter image description here

In this case both strconv and fmt.Sprintf do the same job but using the strconv package's Itoa function is the best choice, because fmt.Sprintf allocate one more object during conversion.

check the nenchmark result of both check the benchmark here: https://gist.github.com/evalphobia/caee1602969a640a4530

see https://play.golang.org/p/hlaz_rMa0D for example.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.