HUGO
Menu
GitHub 90451 stars Mastodon

fmt.Printf

Returns a string formatted according to the given format string.
Syntax
fmt.Printf FORMAT [INPUT]
Returns
string
Alias
printf

Usage

The fmt.Printf function returns a string formatted according to the given format string. The function replaces each verb in FORMAT, such as %s or %d, with the corresponding value from INPUT.

The documentation for Go’s fmt package describes the structure and content of the format string.

Examples

{{ $var := "world" }}
{{ fmt.Printf "Hello %s." $var }} → Hello world.

Formatting a floating-point number:

{{ $pi := 3.14159265 }}
{{ fmt.Printf "Pi is approximately %.2f." $pi }} → Pi is approximately 3.14.

Creating an HTML attribute with the safe.HTMLAttr function:

{{ $desc := "Eat at Joe's" }}
<meta name="description" {{ fmt.Printf "content=%q" $desc | safeHTMLAttr }}>

The rendered output:

<meta name="description" content="Eat at Joe's">