How to Build a String with fmt.Sprintf in Go
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
<text> := fmt.Sprintf(<format>, <value1>, <value2>)
| Part | What 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:
| Verb | What it writes |
|---|---|
%s | "the uninterpreted bytes of the string or slice" |
%d | A 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 runcompiles and runs a single file, andgo vetcomes with Go. This guide uses Go 1.27.4.
Step 1: Write the Code
Create a file named 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
go run main.go
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:
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)
}
go run main.go
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:
go vet main.go
main.go:10:35: fmt.Sprintf format %d has arg plan of wrong type string

What Happened
- The verbs took the values in order.
%stookplanand%dtooklimit, so the message reads "over the pro plan's order limit of 500". fmt.Sprintfprinted nothing itself. It returned the text, the program stored it inmessage, andfmt.Printlnprinted it.len(message)is38, the number of bytes in the text.%vwrote each value in its default format, and%qput the string in double quotes:pro, 500, "pro".- With
%dfor 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 vetfound the mistake without running the program.main.go:10:35is line 10, column 35: the%dverb 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
| Mistake | What happens | Do this instead |
|---|---|---|
Calling fmt.Sprintf without using its result | Nothing is printed, because it only returns the text. go vet says result of fmt.Sprintf call not used | Use the result, or call fmt.Printf to print |
A verb that doesn't fit its value, such as %d for a string | The text holds %!d(string=pro) and the program carries on | Use %s for strings and %d for whole numbers, or %v for any value |
| Fewer values than verbs | The text holds %!d(MISSING), and go vet says fmt.Sprintf format %d reads arg #2, but call has 1 arg | Pass one value per verb |
| More values than verbs | The text ends with %!(EXTRA int=500) | Remove the extra value, or add a verb for it |
Writing a bare % for a percent sign | It 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
| Symptom | Likely cause | Fix |
|---|---|---|
%!d(string=pro) in the text | %d was given a string | Use %s or %v |
%!d(MISSING) in the text | There are fewer values than verbs | Add the missing value |
%!(EXTRA int=500) at the end of the text | There are more values than verbs | Remove the value, or add a verb |
go vet says fmt.Sprintf format %d has arg plan of wrong type string | The verb doesn't fit the value | Change 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
- fmt.Sprintf and the fmt package overview, which lists every verb and the format errors.
- go vet, in the Go documentation.
