Go fmt.Sprintf format string
Go can use fmt.Sprintf to format strings, as follows:
fmt.Sprintf(格式化样式, 参数列表…)
- Formatting style:In string form, the formatting symbols are indicated by%At the beginning, %s is the string format, and %d is the decimal integer format.
- Parameter list:Multiple parameters are separated by commas, and the number of parameters must correspond one-to-one with the number of format specifiers in the format string; otherwise, a runtime error will be reported.
Example
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, ignore some errors
}
The output result is:
Code=000987&endDate=2020-12-31 Kim is 22 years old.
Example
import (
"fmt"
"io"
"os"
)
func main() {
// Declare numeric variables
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 result is:
5 + 10 = 15
Go string formatting symbols:
| Format | Description |
|---|---|
| %v | Outputs the value as-is |
| %+v | Building on %v, expand the struct field names and values. |
| %#v | Outputs the value in Go language syntax format |
| %T | Outputs the type and value in Go syntax format. |
| %% | Output % itself |
| %b | Displays integers in binary |
| %o | Displays integers in octal |
| %d | Displays integers in decimal |
| %x | Displays integers in hexadecimal |
| %X | Display integers in hexadecimal with uppercase letters. |
| %U | Unicode character |
| %f | Floating-point number |
| %p | Pointer, displayed in hexadecimal |
Example
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, %s, %d, and %.2f in the format string "Name: %s, Age: %d, Height: %.2f" are respectively replaced by the values of the name, age, and height variables, where%.2fRepresents a floating-point format with two decimal places.
The output result is:
Name: John, Age: 30, Height: 175.50
%.2fIs a floating-point formatting option used to control the decimal precision of floating-point numbers, where2Indicates the number of decimal places to keep, and you can modify it as needed.
More usage examples:
Example
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 result 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.
Commonly used alignment parameters include:
%s: For string format, the following alignment parameters can be used:%s: Default alignment, left-aligned.%10s: Specifies right-alignment with a width of 10.%-10s: Specifies left-alignment with a width of 10.
%d: For integer format, the following alignment parameters can be used:%d: Default alignment, right-aligned.%10d: Specifies right-alignment with a width of 10.%-10d: Specifies left-alignment with a width of 10.
%f: For floating-point format, the following alignment parameters can be used:%f: Default alignment, right-aligned.%10f: Specifies right-alignment with a width of 10.%-10f: Specifies left-alignment with a width of 10.
Example
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 result is:
|Hello| | Hello| |Hello | |42| | 42| |42 | |3.141590| | 3.141590| |3.141590 |other extensions