Go fmt.Printf Formatting Strings
fmt.Printf is a powerful output formatting function in Go, mainly used to format strings and output the result to standard output (usually the console).
fmt.Printf formats subsequent variables according to the specified format string; the syntax is as follows:
fmt.Printf(格式化样式, 参数列表…)
- **Format specifier:** in string form, format symbols begin with %, where %s is the string format and %d is the decimal integer format.
- **Argument list:** multiple arguments are separated by commas, and their count must correspond one-to-one with the specifiers in the format string, otherwise a runtime error will occur.
Example
package
main
import
(
"fmt"
"strings"
)
func
main
()
{
name
:=
"John Doe"
age
:=
27
// Print the string in lowercase
fmt
.
Printf
(
"hello, %s\n"
,
strings
.
ToLower
(
name
))
// Print the string in uppercase
fmt
.
Printf
(
"hello, %s\n"
,
strings
.
ToUpper
(
name
))
// Print the age as a decimal number
fmt
.
Printf
(
"%s is %d years old\n"
,
name
,
age
)
// Print the age as a floating-point number with two decimal places
fmt
.
Printf
(
"%s is %.2f years old\n"
,
name
,
age
)
}
The output is:
hello, John Doe
hello, JOHN DOE
John Doe is 27 years old
John Doe is %!f(int=27) years old
Go string formatting symbols:
| Format symbol | Description | Example |
|---|---|---|
| General format | ||
%v |
Output the variable in the default format | fmt.Printf("%v", 42) |
%+v |
Output a struct with field names included | fmt.Printf("%+v", struct{A int}{A: 42}) |
%#v |
Output formatted in Go syntax | fmt.Printf("%#v", map[string]int{"a": 1}) |
%T |
Output the type of the value | fmt.Printf("%T", 42) |
%% |
Output a percent sign | fmt.Printf("%%") |
| Boolean | ||
%t |
Output true or false |
fmt.Printf("%t", true) |
| Integers | ||
%b |
Binary representation | fmt.Printf("%b", 5) |
%c |
The corresponding Unicode character | fmt.Printf("%c", 65) |
%d |
Decimal representation | fmt.Printf("%d", 42) |
%o |
Octal representation | fmt.Printf("%o", 10) |
%x |
Hexadecimal representation (lowercase letters) | fmt.Printf("%x", 15) |
%X |
Hexadecimal representation (uppercase letters) | fmt.Printf("%X", 15) |
%U |
Output in Unicode format | fmt.Printf("%U", 65) |
| Float (floating point number) | ||
%f |
Decimal floating-point number | fmt.Printf("%f", 3.14) |
%e |
Scientific notation (lowercase e) | fmt.Printf("%e", 3.14) |
%E |
Scientific notation (uppercase E) | fmt.Printf("%E", 3.14) |
%g |
Automatic %f or %e the concise representation of |
fmt.Printf("%g", 3.14) |
| Strings and bytes | ||
%s |
Ordinary string | fmt.Printf("%s", "Go") |
%q |
A string or character with double quotes | fmt.Printf("%q", "Go") |
%x |
Each byte represented by two hexadecimal characters | fmt.Printf("%x", "abc") |
%X |
Hexadecimal (uppercase) representation | fmt.Printf("%X", "abc") |
| pointer | ||
%p |
Pointer address | fmt.Printf("%p", &x) |
A format string consists of regular text and formatting placeholders.
A formatting placeholder begins with % followed by one or more characters that specify the format type.
The structure of a formatting placeholder is:
%[flags][width][.precision]verb
- flags: flags for controlling formatted output (optional).
-: left-align.+: always show the sign of the number.0: pad with zeros.#: add a prefix for binary, octal, hexadecimal, etc.空格: add a space before positive numbers, and a minus sign before negative numbers-。
- width: output width (optional).
- .precision: number of digits after the decimal point for floating-point numbers (optional).
- verb: used to specify how the data is formatted.
** 1. Formatting integers**
fmt.Printf("十进制: %d, 二进制: %b, 八进制: %o, 十六进制: %x\n", 42, 42, 42, 42)
// Output: Decimal: 42, Binary: 101010, Octal: 52, Hexadecimal: 2a
2. Formatting floating-point numbers
fmt.Printf("普通: %f, 科学计数法: %e, 自动: %g\n", 123.456, 123.456, 123.456)
// Output: Normal: 123.456000, Scientific Notation: 1.234560e+02, Automatic: 123.456
3. Formatting strings
fmt.Printf("普通字符串: %s, 带引号: %q, 十六进制: %x\n", "Go", "Go", "Go")
// Output: Plain string: Go, Quoted: "Go", Hexadecimal: 476f
4. Using width and alignment
fmt.Printf("|%10s|%-10s|\n", "right", "left")
// 输出:| right|left |
5. Formatting structs
type User struct {
Name string
Age int
}
user := User{Name: "Alice", Age: 30}
fmt.Printf("默认: %v\n带字段名: %+v\nGo语法: %#v\n", user, user, user)
// 输出:
// Default: {Alice 30}
// With field names: {Name:Alice Age:30}
// Go syntax: main.User{Name:"Alice", Age:30}
6. Formatting booleans
fmt.Printf("布尔值: %t, %t\n", true, false)
// Output: boolean: true, false
Example
package
main
import
(
"fmt"
"os"
)
type
point
struct
{
x
,
y
int
}
func
main
()
{
p
:=
point
{
1
,
2
}
fmt
.
Printf
(
"%v\n"
,
p
)
fmt
.
Printf
(
"%+v\n"
,
p
)
fmt
.
Printf
(
"%#v\n"
,
p
)
fmt
.
Printf
(
"%T\n"
,
p
)
fmt
.
Printf
(
"%t\n"
,
true
)
fmt
.
Printf
(
"%d\n"
,
123
)
fmt
.
Printf
(
"%b\n"
,
14
)
fmt
.
Printf
(
"%c\n"
,
33
)
fmt
.
Printf
(
"%x\n"
,
456
)
fmt
.
Printf
(
"%f\n"
,
78
.
9
)
fmt
.
Printf
(
"%e\n"
,
123400000
.
0
)
fmt
.
Printf
(
"%E\n"
,
123400000
.
0
)
fmt
.
Printf
(
"%s\n"
,
""string""
)
fmt
.
Printf
(
"%q\n"
,
""string""
)
fmt
.
Printf
(
"%x\n"
,
"hex this"
)
fmt
.
Printf
(
"%p\n"
,
&
p
)
fmt
.
Printf
(
"|%6d|%6d|\n"
,
12
,
345
)
fmt
.
Printf
(
"|%6.2f|%6.2f|\n"
,
1
.
2
,
3
.
45
)
fmt
.
Printf
(
"|%-6.2f|%-6.2f|\n"
,
1
.
2
,
3
.
45
)
fmt
.
Printf
(
"|%6s|%6s|\n"
,
"foo"
,
"b"
)
fmt
.
Printf
(
"|%-6s|%-6s|\n"
,
"foo"
,
"b"
)
s
:=
fmt
.
Sprintf
(
"a %s"
,
"string"
)
fmt
.
Println
(
s
)
fmt
.
Fprintf
(
os
.
Stderr
,
"an %s\n"
,
"error"
)
}
The output is:
{1 2}
{x:1 y:2}
main.point{x:1, y:2}
main.point
true
123
1110
!
1c8
78.900000
1.234000e+08
1.234000E+08
"string"
"\"string\""
6865782074686973
0xc0000b4010
| 12| 345|
| 1.20| 3.45|
|1.20 |3.45 |
| foo| b|
|foo |b |
a string
an error
Go Language Basic Syntax