content
1. Generic placeholders (print different types)
4. Floating point and complex numbers
The *printf series functions of the fmt package all support the format format parameter. Here, we divide the variable type to be replaced by the placeholder, which is convenient for query and memory.
1. Generic placeholders (print different types)
Placeholder |
illustrate |
%v |
The default format representation of the value |
%+v |
Similar to %v, but adds field names when outputting structs |
%#v |
Go syntax representation of value |
%T |
type of print value |
%% |
percent sign |
The sample code is as follows:
fmt.Printf("%v\n", 100)
fmt.Printf("%v\n", false)
o := struct{
name string
}{"枯藤"}
fmt.Printf("%v\n", o)
fmt.Printf("%#v\n", o)
fmt.Printf("%T\n", o)
fmt.Printf("100%%\n")
//输出结果如下:
100
false
{枯藤}
struct { name string }{name:"枯藤"}
struct { name string }
100%
2. Boolean
Placeholder |
illustrate |
%t |
true or false |
3. Integer
Placeholder |
illustrate |
%b |
represented as binary |
%c |
The unicode code value corresponding to this value |
%d |
expressed in decimal |
%o |
expressed in octal |
%x |
Represented as hexadecimal, use af |
%X |
Represented as hexadecimal, use AF |
%U |
Represented in Unicode format: U+1234, equivalent to "U+%04X" |
%q |
The literal value of the go syntax character enclosed in single quotes corresponding to this value will be safely escaped if necessary |
The sample code is as follows:
n := 65
fmt.Printf("%b\n", n)
fmt.Printf("%c\n", n)
fmt.Printf("%d\n", n)
fmt.Printf("%o\n", n)
fmt.Printf("%x\n", n)
fmt.Printf("%X\n", n)
The output is as follows:
1000001
A
65
101
41
41
4. Floating point and complex numbers
Placeholder |
illustrate |
%b |
Scientific notation with no fractional part, binary exponent, such as -123456p-78 |
%e |
Scientific notation, such as -1234.456e+78 |
%E |
Scientific notation, such as -1234.456E+78 |
%f |
Has a fractional part but no exponent part, such as 123.456 |
%F |
Equivalent to %f |
%g |
Use %e or %f format as appropriate (for more concise and accurate output) |
%G |
Use %E or %F format depending on the actual situation (for more concise and accurate output) |
The sample code is as follows:
f := 12.34
fmt.Printf("%b\n", f)
fmt.Printf("%e\n", f)
fmt.Printf("%E\n", f)
fmt.Printf("%f\n", f)
fmt.Printf("%g\n", f)
fmt.Printf("%G\n", f)
//输出结果如下:
6946802425218990p-49
1.234000e+01
1.234000E+01
12.340000
12.34
12.34
5. String and []byte
Placeholder |
illustrate |
%s |
Direct output string or []byte |
%q |
The literal value of the go syntax string enclosed in double quotes corresponding to the value, which will be safely escaped if necessary |
%x |
Each byte is represented by a two-character hexadecimal number (using af |
%X |
Each byte is represented by a two-character hexadecimal number (using AF) |
The sample code is as follows:
s := "枯藤"
fmt.Printf("%s\n", s)
fmt.Printf("%q\n", s)
fmt.Printf("%x\n", s)
fmt.Printf("%X\n", s)
//输出结果如下:
枯藤
"枯藤"
e69eafe897a4
E69EAFE897A4
6. Pointer
Placeholder |
illustrate |
%p |
Represented as hexadecimal with a leading 0x |
The sample code is as follows:
a := 18
fmt.Printf("%p\n", &a)
fmt.Printf("%#p\n", &a)
输出结果如下:
0xc000054058
c000054058
7. Width identifier
The width is specified by a decimal number immediately following the percent sign, if no width is specified, the value is not padded except required. Precision is specified by an (optional) width followed by a period followed by a decimal number. If no precision is specified, the default precision is used; if the period is not followed by a digit, the precision is 0. Examples are as follows
Placeholder |
illustrate |
%f |
Default width, default precision |
%9f |
width 9, default precision |
%.2f |
Default width, precision 2 |
%9.2f |
width 9, precision 2 |
%9.f |
width 9, precision 0 |
The sample code is as follows:
n := 88.88
fmt.Printf("%f\n", n)
fmt.Printf("%9f\n", n)
fmt.Printf("%.2f\n", n)
fmt.Printf("%9.2f\n", n)
fmt.Printf("%9.f\n", n)
/输出结果如下:
88.880000
88.880000
88.88
88.88
89
8. Other falg
Placeholder |
illustrate |
’+’ |
Always output the sign of the value; %q (%+q) will produce output that is all ASCII characters (by escaping); |
’ ‘ |
对数值,正数前加空格而负数前加负号;对字符串采用%x或%X时(% x或% X)会给各打印的字节之间加空格 |
’-’ |
在输出右边填充空白而不是默认的左边(即从默认的右对齐切换为左对齐); |
’#’ |
八进制数前加0(%#o),十六进制数前加0x(%#x)或0X(%#X),指针去掉前面的0x(%#p)对%q(%#q),对%U(%#U)会输出空格和单引号括起来的go字面值; |
‘0’ |
使用0而不是空格填充,对于数值类型会把填充的0放在正负号后面; |
举个例子:
s := "枯藤"
fmt.Printf("%s\n", s)
fmt.Printf("%5s\n", s)
fmt.Printf("%-5s\n", s)
fmt.Printf("%5.7s\n", s)
fmt.Printf("%-5.7s\n", s)
fmt.Printf("%5.2s\n", s)
fmt.Printf("%05s\n", s)
输出结果如下:
枯藤
枯藤
枯藤
枯藤
枯藤
枯藤
000枯藤