Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Go Dictionary
  3. fmt.Sprintf() / Fprintf()

fmt.Sprintf() / Fprintf()

fmt.Sprintf() returns a formatted string without printing it. Use it when you need the result as a string value. fmt.Fprintf() writes to an io.Writer, making it useful for writing to files or standard error.

Syntax

import (
    "fmt"
    "os"
)

// Returns a formatted string (does not print it).
s := fmt.Sprintf("format string", val1, val2, ...)

// Writes formatted output to an io.Writer.
n, err := fmt.Fprintf(writer, "format string", val1, val2, ...)

// Writing to standard error (common pattern)
fmt.Fprintf(os.Stderr, "error: %v\n", err)

// Related functions
s := fmt.Sprintf(...)   // returns a string
fmt.Fprintln(w, ...)    // writes to io.Writer with a newline
fmt.Fprint(w, ...)      // writes to io.Writer without a newline

Function List

FunctionDescription
fmt.Sprintf(format, a ...any) stringReturns a formatted string. Does not print anything.
fmt.Sprintf("%v", v)The most general-purpose way to convert any value to a string.
fmt.Fprintf(w, format, a ...any)Writes formatted output to an io.Writer. Returns the number of bytes written and any error.
fmt.Fprintln(w, a ...any)Writes arguments to an io.Writer separated by spaces, with a trailing newline.
fmt.Fprint(w, a ...any)Writes arguments to an io.Writer.

Sample Code

package main

import (
    "fmt"
    "os"
    "strings"
)

func main() {
    // Sprintf: build a string and store it in a variable.
    name := "Tokyo"
    temp := 25.5
    message := fmt.Sprintf("Temperature in %s is %.1f°C.", name, temp)
    fmt.Println(message)

    // Build a string dynamically.
    items := []string{"Apple", "Banana", "Orange"}
    var parts []string
    for i, item := range items {
        parts = append(parts, fmt.Sprintf("%d. %s", i+1, item))
    }
    fmt.Println(strings.Join(parts, "\n"))

    fmt.Println()

    // Building an error message (compare with fmt.Errorf)
    id := 404
    errMsg := fmt.Sprintf("resource with ID %d not found", id)
    fmt.Println("Error:", errMsg)

    fmt.Println()

    // Fprintf: write to an io.Writer.
    // Write to standard output (os.Stdout).
    fmt.Fprintf(os.Stdout, "Writing with Fprintf: %s\n", "test")

    // Write to a string buffer using strings.Builder.
    var sb strings.Builder
    for i := 1; i <= 5; i++ {
        fmt.Fprintf(&sb, "line %d\n", i)
    }
    fmt.Print(sb.String())

    // Write to standard error (useful for error logging).
    // fmt.Fprintf(os.Stderr, "error: %v\n", someError)
}

Notes

fmt.Sprintf() is the most versatile string formatting function. To convert any value to a string, fmt.Sprintf("%v", value) is a convenient choice. However, it has more overhead than simple string concatenation, so consider using strings.Builder in hot paths.

Repeatedly calling Sprintf inside a loop and concatenating the results is inefficient. Writing incrementally to a strings.Builder with fmt.Fprintf, or using strings.Join(), is better for performance.

For printing to standard output, see fmt.Println() / Printf(). For a list of format verbs, see Format Verbs.

If you find any errors or copyright issues, please .