This is a creation in Article, where the information may have evolved or changed.
The standard output stream of the go language differs from other languages (such as C # and Java) when printing to the screen, and here are some of the most common formatting input operations I've compiled.
General
%v
Print the value of a variable in the default way
%T
Types of print variables
Integer
%+d
Signed integral type, fmt.Printf("%+d", 255)
output+255
%q
Print single quotation marks
%o
Octal with no 0
%#o
Octal with 0
%x
lowercase hexadecimal
%X
Hexadecimal in uppercase
%#x
Hex with 0x
%U
Print Unicode characters
%#U
To print Unicode with characters
%b
Binary for printing integer type
Integer width
%5d
Indicates that the maximum length of the integer is 5, and the following code
fmt.Printf("|%5d|", 1) fmt.Printf("|%5d|", 1234567)
The output results are as follows:
| 1| | 1234567|
%-5d
Instead, the print results are automatically left-justified
%05d
0 is prepended to the number.
Float
%f
(=%.6f
) 6-bit decimal point
%e
(=%.6e
) 6-bit decimal point (scientific notation)
%g
Use the fewest numbers to represent
%.3g
Up to 3 digits to indicate
%.3f
Up to 3 decimal places to represent
String
%s
Normal output string
%q
string with double quotation marks, quotes with escape characters in the string
%#q
String with anti-quotation marks, if the string has anti-quotes, use double quotation marks instead of
%x
Convert a string to lowercase 16 binary format
%X
Convert a string to uppercase 16 binary format
% x
16 binary format with spaces
String Width (example with 5)
%5s
Minimum width of 5
%-5s
Minimum width is 5 (left justified)
%.5s
Maximum width of 5
%5.7s
The minimum width is 5 and the maximum width is 7
%-5.7s
The minimum width is 5 and the maximum width is 7 (left-justified)
%5.3s
Truncated if the width is greater than 3
%05s
If the width is less than 5, the string is preceded by 0
Struct
%v
Normal printing. Like what:{sam {12345 67890}}
%+v
With the field name. Like what:{name:sam phone:{mobile:12345 office:67890}
%#v
Use go syntax to print.
Like whatmain.People{name:”sam”, phone:main.Phone{mobile:”12345”, office:”67890”}}
Boolean
Pointer
%p
With a 0x pointer
%#p
Without a 0x pointer
Reference
- FMT. Printf format Reference (cheat sheet)