| 1 |
--- |
| 2 |
title: fmt.Printf |
| 3 |
description: Formats a string using the standard `fmt.Sprintf` function. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [printf] |
| 9 |
returnType: string |
| 10 |
signatures: ['fmt.Printf FORMAT [INPUT]'] |
| 11 |
aliases: [/functions/printf] |
| 12 |
--- |
| 13 |
|
| 14 |
{{% include "/_common/functions/fmt/format-string.md" %}} |
| 15 |
|
| 16 |
```go-html-template |
| 17 |
{{ $var := "world" }} |
| 18 |
{{ printf "Hello %s." $var }} → Hello world. |
| 19 |
``` |
| 20 |
|
| 21 |
```go-html-template |
| 22 |
{{ $pi := 3.14159265 }} |
| 23 |
{{ printf "Pi is approximately %.2f." $pi }} → 3.14 |
| 24 |
``` |
| 25 |
|
| 26 |
Use the `printf` function with the [`safe.HTMLAttr`][] function: |
| 27 |
|
| 28 |
```go-html-template |
| 29 |
{{ $desc := "Eat at Joe's" }} |
| 30 |
<meta name="description" {{ printf "content=%q" $desc | safeHTMLAttr }}> |
| 31 |
``` |
| 32 |
|
| 33 |
Hugo renders this to: |
| 34 |
|
| 35 |
```html |
| 36 |
<meta name="description" content="Eat at Joe's"> |
| 37 |
``` |
| 38 |
|
| 39 |
[`safe.HTMLAttr`]: /functions/safe/htmlattr/ |