By Carnegie

2012-06-20 16:21:34 8 Comments

Is there a simple way to format a string in Go without printing the string?

I can do:

bar := "bar"
fmt.Printf("foo: %s", bar)

But I want the formatted string returned rather than printed so I can manipulate it further.

I could also do something like:

s := "foo: " + bar

But this becomes difficult to read when the format string is complex, and cumbersome when one or many of the parts aren't strings and have to be converted first, like

i := 25
s := "foo: " + strconv.Itoa(i)

Is there a simpler way to do this?


@Sonia 2012-06-20 16:43:59

Sprintf is what you are looking for.


fmt.Sprintf("foo: %s", bar)

You can also see it in use in the Errors example as part of "A Tour of Go."

return fmt.Sprintf("at %v, %s", e.When, e.What)

@Filip Bartuzi 2017-03-28 14:11:32

does letter after % matter? Could it be %y and %q ? or %y and %y

@raulsntos 2017-04-08 11:48:24

The letter does matter, it's called a verb, basically it lets Sprintf know what type the variable is so that if it receives 65 and the verb is %d it will print the number 65 but if the verb is %c it will print the character 'A'. See:

@Mo-Gang 2018-06-20 06:47:31

fmt.SprintF function returns a string and you can format the string the very same way you would have with fmt.PrintF

@icza 2015-07-31 09:06:01

1. Simple strings

For "simple" strings (typically what fits into a line) the simplest solution is using fmt.Sprintf() and friends (fmt.Sprint(), fmt.Sprintln()). These are analogous to the functions without the starter S letter, but these Sxxx() variants return the result as a string instead of printing them to the standard output.

For example:

s := fmt.Sprintf("Hi, my name is %s and I'm %d years old.", "Bob", 23)

The variable s will be initialized with the value:

Hi, my name is Bob and I'm 23 years old.

Tip: If you just want to concatenate values of different types, you may not automatically need to use Sprintf() (which requires a format string) as Sprint() does exactly this. See this example:

i := 23
s := fmt.Sprint("[age:", i, "]") // s will be "[age:23]"

For concatenating only strings, you may also use strings.Join() where you can specify a custom separator string (to be placed between the strings to join).

Try these on the Go Playground.

2. Complex strings (documents)

If the string you're trying to create is more complex (e.g. a multi-line email message), fmt.Sprintf() becomes less readable and less efficient (especially if you have to do this many times).

For this the standard library provides the packages text/template and html/template. These packages implement data-driven templates for generating textual output. html/template is for generating HTML output safe against code injection. It provides the same interface as package text/template and should be used instead of text/template whenever the output is HTML.

Using the template packages basically requires you to provide a static template in the form of a string value (which may be originating from a file in which case you only provide the file name) which may contain static text, and actions which are processed and executed when the engine processes the template and generates the output.

You may provide parameters which are included/substituted in the static template and which may control the output generation process. Typical form of such parameters are structs and map values which may be nested.


For example let's say you want to generate email messages that look like this:

Hi [name]!

Your account is ready, your user name is: [user-name]

You have the following roles assigned:
[role#1], [role#2], ... [role#n]

To generate email message bodies like this, you could use the following static template:

const emailTmpl = `Hi {{.Name}}!

Your account is ready, your user name is: {{.UserName}}

You have the following roles assigned:
{{range $i, $r := .Roles}}{{if ne $i 0}}, {{end}}{{.}}{{end}}

And provide data like this for executing it:

data := map[string]interface{}{
    "Name":     "Bob",
    "UserName": "bob92",
    "Roles":    []string{"dbteam", "uiteam", "tester"},

Normally output of templates are written to an io.Writer, so if you want the result as a string, create and write to a bytes.Buffer (which implements io.Writer). Executing the template and getting the result as string:

t := template.Must(template.New("email").Parse(emailTmpl))
buf := &bytes.Buffer{}
if err := t.Execute(buf, data); err != nil {
s := buf.String()

This will result in the expected output:

Hi Bob!

Your account is ready, your user name is: bob92

You have the following roles assigned:
dbteam, uiteam, tester

Try it on the Go Playground.

Also note that since Go 1.10, a newer, faster, more specialized alternative is available to bytes.Buffer which is: strings.Builder. Usage is very similar:

builder := &strings.Builder{}
if err := t.Execute(builder, data); err != nil {
s := builder.String()

Try this one on the Go Playground.

Note: you may also display the result of a template execution if you provide os.Stdout as the target (which also implements io.Writer):

t := template.Must(template.New("email").Parse(emailTmpl))
if err := t.Execute(os.Stdout, data); err != nil {

This will write the result directly to os.Stdout. Try this on the Go Playground.

@Kabeer Shaikh 2018-01-23 02:55:27

In your case, you need to use Sprintf() for format string.

func Sprintf(format string, a ...interface{}) string

Sprintf formats according to a format specifier and returns the resulting string.

s := fmt.Sprintf("Good Morning, This is %s and I'm living here from last %d years ", "John", 20)

Your output will be :

Good Morning, This is John and I'm living here from last 20 years.

Related Questions

Sponsored Content

63 Answered Questions

[SOLVED] What is the difference between String and string in C#?

84 Answered Questions

[SOLVED] How do I make the first letter of a string uppercase in JavaScript?

3 Answered Questions

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

56 Answered Questions

[SOLVED] How to replace all occurrences of a string?

16 Answered Questions

[SOLVED] String formatting: % vs. .format

24 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

18 Answered Questions

[SOLVED] Why is char[] preferred over String for passwords?

Sponsored Content