Sprintf and Fprintf
Format to strings and writers.
Sprintf and Fprintf is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Printf Family
The fmt package has three formatting cousins:
Printfwrites to standard output.Sprintfreturns a formatted string.Fprintfwrites to any writer.
package main
import "fmt"
func main() {
fmt.Printf("hello %s\n", "world")
}Sprintf Returns a String
fmt.Sprintf formats exactly like Printf but, instead of printing, it returns the result as a string you can store or reuse.
package main
import "fmt"
func main() {
msg := fmt.Sprintf("%s scored %d", "Bob", 95)
fmt.Println(msg)
}Building Identifiers
Sprintf is handy for assembling keys, file names, or labels from parts.
package main
import "fmt"
func main() {
id := fmt.Sprintf("user-%04d", 42)
fmt.Println(id)
}Formatting Numbers into Text
Use Sprintf to turn numbers into nicely formatted strings, such as currency.
package main
import "fmt"
func main() {
label := fmt.Sprintf("Price: $%.2f", 7.5)
fmt.Println(label)
}Fprintf Writes to a Writer
fmt.Fprintf takes an io.Writer as its first argument and formats into it. os.Stdout is one such writer.
package main
import (
"fmt"
"os"
)
func main() {
fmt.Fprintf(os.Stdout, "to stdout: %d\n", 1)
}Writing to Standard Error
Send diagnostics to os.Stderr with Fprintf so they stay separate from normal output.
package main
import (
"fmt"
"os"
)
func main() {
fmt.Fprintf(os.Stderr, "warning: %s\n", "low disk")
}Fprintf into a Builder
Because strings.Builder is also an io.Writer, you can format directly into it and read the string at the end.
package main
import (
"fmt"
"strings"
)
func main() {
var b strings.Builder
fmt.Fprintf(&b, "%s=%d;", "x", 10)
fmt.Fprintf(&b, "%s=%d", "y", 20)
fmt.Println(b.String())
}Fprintf into a bytes.Buffer
A bytes.Buffer is another common writer target. Build up content, then convert to a string.
package main
import (
"bytes"
"fmt"
)
func main() {
var buf bytes.Buffer
fmt.Fprintf(&buf, "row %d\n", 1)
fmt.Fprintf(&buf, "row %d\n", 2)
fmt.Print(buf.String())
}Sprint and Fprint Without f
The f versions take a format string. Drop the f for Sprint/Fprint, which simply concatenate arguments with default formatting.
package main
import "fmt"
func main() {
s := fmt.Sprint("a", 1, true)
fmt.Println(s)
}Sprintln Adds Spaces
fmt.Sprintln joins arguments with spaces and appends a newline, returning the string.
package main
import "fmt"
func main() {
s := fmt.Sprintln("x", 1, "y", 2)
fmt.Print(s)
}Choosing the Right Tool
Pick based on the destination:
- Need a string? Use
Sprintf. - Writing to stdout? Use
Printf. - Writing to a file, buffer, or error stream? Use
Fprintf.
package main
import (
"fmt"
"os"
)
func main() {
s := fmt.Sprintf("count=%d", 3)
fmt.Fprintf(os.Stdout, "%s\n", s)
}Quick Check
Which function returns the formatted text instead of printing it?
Recap: Sprintf and Fprintf
You learned the formatting family:
Sprintfreturns a string.Fprintfwrites to anyio.Writer(stdout, stderr, buffers, builders).- The non-
fvariants use default formatting.
package main
import (
"fmt"
"strings"
)
func main() {
var b strings.Builder
fmt.Fprintf(&b, "result=%s", fmt.Sprintf("%d%%", 80))
fmt.Println(b.String())
}Frequently asked questions
Is the “Sprintf and Fprintf” lesson free?
Yes — the full text of “Sprintf and Fprintf” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Sprintf and Fprintf”?
Format to strings and writers. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Sprintf and Fprintf” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Printf Verbs
- Sprintf and Fprintf
- The Stringer Interface
- Scanning Input