Skip to main content

How to Build a String with fmt.Sprintf in Go

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

fmt.Sprintf(format, values...) builds a string. It replaces each verb in the format, such as %s for a string or %d for a whole number, with the next value, and returns the text instead of printing it. A verb that doesn't fit its value doesn't stop the program: it writes a marker such as %!d(string=pro) into the text, which go vet catches before you run it.

Syntax​

Syntax
<text> := fmt.Sprintf(<format>, <value1>, <value2>)
PartWhat it does
<format>The text to build, with a verb such as %s or %d where each value goes.
<value1>, <value2>The values, in the order of the verbs. fmt.Sprintf takes any number of them.
<text>The finished string.

The verbs this guide uses, from the fmt package documentation:

VerbWhat it writes
%s"the uninterpreted bytes of the string or slice"
%dA whole number in "base 10"
%v"the value in a default format"
%q"a double-quoted string safely escaped with Go syntax"

The documentation of fmt.Sprintf: "Sprintf formats according to a format specifier and returns the resulting string."

The Example​

The example builds an error message from a plan name, pro, and an order limit, 500. It prints the message and its length, then prints the same two values with %v and %q. A changed version then uses %d for the plan name, and go vet checks that version.

Prerequisites​

  • Go. go run compiles and runs a single file, and go vet comes with Go. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which sets plan to pro and limit to 500, builds a message with fmt.Sprintf and the verbs %s and %d, prints the message and its length, then prints the plan and the limit with %v and the plan with %q.

main.go
package main

import "fmt"

func main() {
plan := "pro"
limit := 500

// Sprintf returns the text instead of printing it.
message := fmt.Sprintf("over the %s plan's order limit of %d", plan, limit)
fmt.Println(message)
fmt.Println(len(message))

// %v writes any value in its default format, and %q a string in double quotes.
fmt.Println(fmt.Sprintf("%v, %v, %q", plan, limit, plan))
}

Step 2: Run It​

Terminal
go run main.go
Output
over the pro plan's order limit of 500
38
pro, 500, "pro"

The command is the same in zsh, bash and PowerShell.

Step 3: Use a Verb That Doesn't Fit​

Replace main.go with a version that uses %d, the verb for whole numbers, for the plan name:

main.go
package main

import "fmt"

func main() {
plan := "pro"
limit := 500

// %d is for whole numbers, but plan is a string.
message := fmt.Sprintf("over the %d plan's order limit of %d", plan, limit)
fmt.Println(message)
}
Terminal
go run main.go
Output
over the %!d(string=pro) plan's order limit of 500

Step 4: Check the Code with go vet​

go vet examines Go source code without running it and reports mistakes such as this one. Run it on the changed file:

Terminal
go vet main.go
Output
main.go:10:35: fmt.Sprintf format %d has arg plan of wrong type string

A zsh terminal where go run main.go prints the message about the pro plan's order limit of 500, then 38 and pro, 500, "pro"; the changed file prints the message with %!d(string=pro) in place of the plan, and go vet reports that format %d has arg plan of wrong type string.

What Happened​

  • The verbs took the values in order. %s took plan and %d took limit, so the message reads "over the pro plan's order limit of 500".
  • fmt.Sprintf printed nothing itself. It returned the text, the program stored it in message, and fmt.Println printed it. len(message) is 38, the number of bytes in the text.
  • %v wrote each value in its default format, and %q put the string in double quotes: pro, 500, "pro".
  • With %d for a string, the program still ran, but the text holds %!d(string=pro) where the plan should be. The fmt documentation calls this a format error: "Wrong type or unknown verb: %!verb(type=value)". It adds that "All errors begin with the string "%!" followed sometimes by a single character (the verb) and end with a parenthesized description."
  • go vet found the mistake without running the program. main.go:10:35 is line 10, column 35: the %d verb in the format. Its documentation says that vet "reports suspicious constructs, such as Printf calls whose arguments do not align with the format string".

Common Mistakes​

MistakeWhat happensDo this instead
Calling fmt.Sprintf without using its resultNothing is printed, because it only returns the text. go vet says result of fmt.Sprintf call not usedUse the result, or call fmt.Printf to print
A verb that doesn't fit its value, such as %d for a stringThe text holds %!d(string=pro) and the program carries onUse %s for strings and %d for whole numbers, or %v for any value
Fewer values than verbsThe text holds %!d(MISSING), and go vet says fmt.Sprintf format %d reads arg #2, but call has 1 argPass one value per verb
More values than verbsThe text ends with %!(EXTRA int=500)Remove the extra value, or add a verb for it
Writing a bare % for a percent signIt starts a verb: fmt.Sprintf("%d% done", 80) gave 80%!d(MISSING)one, and fmt.Sprintf("%d%", 80) gave 80%!(NOVERB)Write %%: fmt.Sprintf("%d%%", 80) gave 80%

Troubleshooting​

SymptomLikely causeFix
%!d(string=pro) in the text%d was given a stringUse %s or %v
%!d(MISSING) in the textThere are fewer values than verbsAdd the missing value
%!(EXTRA int=500) at the end of the textThere are more values than verbsRemove the value, or add a verb
go vet says fmt.Sprintf format %d has arg plan of wrong type stringThe verb doesn't fit the valueChange the verb

FAQ​

What's the difference between fmt.Sprintf and fmt.Printf?​

They take the same format and values. fmt.Printf "writes to standard output", and fmt.Sprintf "returns the resulting string", so you can store the text, pass it on or return it.

How do I print a struct with its field names?​

Use %+v. For a struct with a Name of pro and a Limit of 500, %v wrote {pro 500} and %+v wrote {Name:pro Limit:500}. The documentation: "when printing structs, the plus flag (%+v) adds field names".

Why didn't the compiler catch the wrong verb?​

The format is an ordinary string, and fmt.Sprintf accepts values of any type, so the call is valid Go. The verbs are only read while the program runs. go vet reads the format string ahead of time, which is why it found the mistake.

Conclusion​

fmt.Sprintf fills a format's verbs with values, in order, and returns the text. Match each verb to its value, %s for strings and %d for whole numbers or %v for anything, and run go vet, because a wrong verb doesn't stop the program.

Additional Resources​