0Pricing
Go Academy · Lesson

Sprintf and Fprintf

Format to strings and writers.

The Printf Family

The fmt package has three formatting cousins:

  • Printf writes to standard output.
  • Sprintf returns a formatted string.
  • Fprintf writes 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)
}

All lessons in this course

  1. Printf Verbs
  2. Sprintf and Fprintf
  3. The Stringer Interface
  4. Scanning Input
← Back to Go Academy