How to merge multiple strings and int into a single string

Issue

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.

Solution

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

Answered By – evanmcdonnal

Answer Checked By – Candace Johnson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.