Go fmt.Sprintf Formatting Strings
Go can use fmt.Sprintf to format strings, in the following form:
fmt.Sprintf(格式化样式, 参数列表…)
- **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"
"io"
"os"
)
func
main
()
{
// In Go, format a string and assign it to a new string using fmt.Sprintf
// %s represents a string
var
stockcode
=
"000987"
var
enddate
=
"2020-12-31"
var
url
=
"Code=%s&endDate=%s"
var
target_url
=
fmt
.
Sprintf
(
url
,
stockcode
,
enddate
)
fmt
.
Println
(
target_url
)
// Another example, %d represents an integer
const
name
,
age
=
"Kim"
,
22
s
:=
fmt
.
Sprintf
(
"%s is %d years old.\n"
,
name
,
age
)
io
.
WriteString
(
os
.
Stdout
,
s
)
// For simplicity, some errors are ignored
}
The output is:
Code=000987&endDate=2020-12-31
Kim is 22 years old.
Example
package
main
import
(
"fmt"
"io"
"os"
)
func
main
()
{
// Declare a numeric variable
const
num1
,
num2
,
num3
=
5
,
10
,
15
// Call the Sprintf() function
s
:=
fmt
.
Sprintf
(
"%d + %d = %d"
,
num1
,
num2
,
num3
)
// Use the WriteString() function to output the result to the terminal to write the
// "os.Stdout" is the content of the string
io
.
WriteString
(
os
.
Stdout
,
s
)
}
The output is:
5 + 10 = 15
Go string formatting symbols:
| Format | Description |
|---|---|
| %v | Output the value as-is |
| %+v | Based on %v, expand struct field names and values |
| %#v | Output the value in Go syntax format |
| %T | Output the type and value in Go syntax format |
| %% | Output the % itself |
| %b | Integers displayed in binary |
| %o | Integers displayed in octal |
| %d | Integers displayed in decimal |
| %x | Integers displayed in hexadecimal |
| %X | Integers displayed in hexadecimal with uppercase letters |
| %U | Unicode character |
| %f | Float (floating point number) |
| %p | Pointer, displayed in hexadecimal |
Example
package
main
import
"fmt"
func
main
()
{
name
:=
"John"
age
:=
30
height
:=
175
.
5
result
:=
fmt
.
Sprintf
(
"Name: %s, Age: %d, Height: %.2f"
,
name
,
age
,
height
)
fmt
.
Println
(
result
)
}
In the above example, the %s, %d, and %.2f in the format string "Name: %s, Age: %d, Height: %.2f" are replaced by the values of the name, age, and height variables respectively, where %.2f represents a floating-point format with two decimal places.
The output is:
Name: John, Age: 30, Height: 175.50
%.2f is a floating-point formatting option used to control the decimal precision of a floating-point number, where 2 represents the number of decimal places to keep, which you can modify as needed.
More usage examples:
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
Alignment
By using width and alignment parameters in the format string, you can control the alignment of the generated string.
Common alignment parameters are:
%s: string format, the following alignment parameters can be used:%s: default alignment, left-aligned.%10s: right-aligned with a specified width of 10.%-10s: left-aligned with a specified width of 10.
%d: integer format, the following alignment parameters can be used:%d: default alignment, right-aligned.%10d: right-aligned with a specified width of 10.%-10d: left-aligned with a specified width of 10.
%f: floating-point format, the following alignment parameters can be used:%f: default alignment, right-aligned.%10f: right-aligned with a specified width of 10.%-10f: left-aligned with a specified width of 10.
Example
package
main
import
"fmt"
func
main
()
{
str
:=
"Hello"
num
:=
42
floatNum
:=
3
.
14159
fmt
.
Printf
(
"|%s|\n"
,
str
)
fmt
.
Printf
(
"|%10s|\n"
,
str
)
fmt
.
Printf
(
"|%-10s|\n"
,
str
)
fmt
.
Printf
(
"|%d|\n"
,
num
)
fmt
.
Printf
(
"|%10d|\n"
,
num
)
fmt
.
Printf
(
"|%-10d|\n"
,
num
)
fmt
.
Printf
(
"|%f|\n"
,
floatNum
)
fmt
.
Printf
(
"|%10f|\n"
,
floatNum
)
fmt
.
Printf
(
"|%-10f|\n"
,
floatNum
)
}
The output is:
|Hello|
| Hello|
|Hello |
|42|
| 42|
|42 |
|3.141590|
| 3.141590|
|3.141590 |
Go Language Basic Syntax