52

I am a newbie in Go. I can't find any official docs showing how to merge multiple strings into a new string.

What I'm expecting:

Input: "key:", "value", ", key2:", 100

Output: "Key:value, key2:100"

I want to use + to merge strings like in Java and Swift if possible.

2

5 Answers 5

90

I like to use fmt's Sprintf method for this type of thing. It works like Printf in Go or C only it returns a string. Here's an example:

output := fmt.Sprintf("%s%s%s%d", "key:", "value", ", key2:", 100)

Go docs for fmt.Sprintf

Sign up to request clarification or add additional context in comments.

2 Comments

the last one is int number. Can I use %d ?
@RobertYiJiang yea, I'll edit with that in fact. Didn't realize I changed the type of the input there.
26

You can use strings.Join, which is almost 3x faster than fmt.Sprintf. However it can be less readable.

output := strings.Join([]string{"key:", "value", ", key2:", strconv.Itoa(100)}, "")

See https://play.golang.org/p/AqiLz3oRVq

strings.Join vs fmt.Sprintf

BenchmarkFmt-4       2000000           685 ns/op
BenchmarkJoins-4     5000000           244 ns/op

Buffer

If you need to merge a lot of strings, I'd consider using a buffer rather than those solutions mentioned above.

2 Comments

Well, if you use strconv.Itoa then you can as well concatenate strings with +
@ColinPitrat Due to an issue on the go compiler, strings.Join of 2 or 3 elements use +, but above that it creates a byte slice for a more efficient concatenation. If you have more than two elements to concatenate, I would strongly recommend to use strings.Join.
3

You can simply do this:

package main

    import (
        "fmt" 
        "strconv"
    )
    
    func main() {

         
         result:="str1"+"str2"+strconv.Itoa(123)+"str3"+strconv.Itoa(12)
         fmt.Println(result)
         
    }

Using fmt.Sprintf()

var s1="abc"
var s2="def"
var num =100
ans:=fmt.Sprintf("%s%d%s", s1,num,s2);
fmt.Println(ans);

Comments

-1

You can use text/template:

package main

import (
   "text/template"
   "strings"
)

func format(s string, v interface{}) string {
   t, b := new(template.Template), new(strings.Builder)
   template.Must(t.Parse(s)).Execute(b, v)
   return b.String()
}

func main() {
   s := struct{
      Key string
      Key2 int
   }{"value", 100}
   f := format("key:{{.Key}}, key2:{{.Key2}}", s)
   println(f)
}

or fmt.Sprint:

package main
import "fmt"

func main() {
   s := fmt.Sprint("key:", "value", ", key2:", 100)
   println(s)
}

Comments

-1

Here's a simple way to combine string and integer in Go Lang(Version: go1.18.1 Latest)

package main

import (
    "fmt"
    "io"
    "os"
)

func main() {
    const name, age = "John", 26
    s := fmt.Sprintf("%s is %d years old.\n", name, age)
    io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
}

Output:

John is 26 years old.

1 Comment

Reference and document link: go.dev

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.