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
%vPrint the value of a variable in the default way
%TTypes of print variables
Integer
%+dSigned integral type, fmt.Printf("%+d", 255) output+255
%qPrint single quotation marks
%oOctal with no 0
%#oOctal with 0
%xlowercase hexadecimal
%XHexadecimal in uppercase
%#xHex with 0x
%UPrint Unicode characters
%#UTo print Unicode with characters
%bBinary for printing integer type
Integer width
%5dIndicates 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|
%-5dInstead, the print results are automatically left-justified
%05d0 is prepended to the number.
Float
%f(=%.6f) 6-bit decimal point
%e(=%.6e) 6-bit decimal point (scientific notation)
%gUse the fewest numbers to represent
%.3gUp to 3 digits to indicate
%.3fUp to 3 decimal places to represent
String
%sNormal output string
%qstring with double quotation marks, quotes with escape characters in the string
%#qString with anti-quotation marks, if the string has anti-quotes, use double quotation marks instead of
%xConvert a string to lowercase 16 binary format
%XConvert a string to uppercase 16 binary format
% x16 binary format with spaces
String Width (example with 5)
%5sMinimum width of 5
%-5sMinimum width is 5 (left justified)
%.5sMaximum width of 5
%5.7sThe minimum width is 5 and the maximum width is 7
%-5.7sThe minimum width is 5 and the maximum width is 7 (left-justified)
%5.3sTruncated if the width is greater than 3
%05sIf the width is less than 5, the string is preceded by 0
Struct
%vNormal printing. Like what:{sam {12345 67890}}
%+vWith the field name. Like what:{name:sam phone:{mobile:12345 office:67890}
%#vUse go syntax to print.
Like whatmain.People{name:”sam”, phone:main.Phone{mobile:”12345”, office:”67890”}}
Boolean
Pointer
%pWith a 0x pointer
%#pWithout a 0x pointer
Reference
- FMT. Printf format Reference (cheat sheet)