aboutsummaryrefslogtreecommitdiffhomepage
path: root/docs/content/en/functions/fmt/Printf.md
blob: 5d0127460a155c4320c1dba1e6fb6de39573b490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
---
title: fmt.Printf
description: Formats a string using the standard `fmt.Sprintf` function.
categories: []
keywords: []
action:
  aliases: [printf]
  related:
    - functions/fmt/Print
    - functions/fmt/Println
  returnType: string
  signatures: ['fmt.Printf FORMAT [INPUT]']
aliases: [/functions/printf]
---

{{% include "functions/fmt/_common/fmt-layout.md" %}}

```go-html-template
{{ $var := "world" }}
{{ printf "Hello %s." $var }} → Hello world.
```

```go-html-template
{{ $pi := 3.14159265 }}
{{ printf "Pi is approximately %.2f." $pi }} → 3.14
```

Use the `printf` function with the `safeHTMLAttr` function:

```go-html-template
{{ $desc := "Eat at Joe's" }}
<meta name="description" {{ printf "content=%q" $desc | safeHTMLAttr }}>
```

Hugo renders this to:

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