This is a creation in Article, where the information may have evolved or changed.
Package Mainimport"FMT"Import"OS"type Pointstruct{x, yint}func Main () {//go provides a variety of printing options for regular Go value format designs. For example, here you print an instance of the point struct body. P: = point{1,2} fmt. Printf ("%v\n", p)//{1 2}//If the value is a struct, the formatted output of%+V will include the field name of the struct body. Fmt. Printf ("%+v\n", p)//{x:1 Y:2}//The % #v form is the output of the GO syntax for that value. For example, the value of the run source snippet. Fmt. Printf ("% #v \ n", p)//main.point{x:1, Y:2}//the type of value to be printed, using%T. Fmt. Printf ("%t\n", p)//Main.point//Formatting A Boolean value is simple. Fmt. Printf ("%t\n",true)//There are several ways to format the shaping number, using%d for standard decimal formatting. Fmt. Printf ("%d\n",123)//This binary representation of the output. Fmt. Printf ("%b\n", -this outputs the corresponding character for the given integer. Fmt. Printf ("%c\n", -)%x provides hexadecimal encoding. Fmt. Printf ("%x\n",456)//There are also many formatting options for floating-point types. Use%f for the most basic decimal formatting. Fmt. Printf ("%f\n",78.9)//%e and%e format floating-point formats (slightly different) scientific and technical notation representation of the notation. Fmt. Printf ("%e\n",123400000.0) fmt. Printf ("%e\n",123400000.0)//use%s for basic string output. Fmt. Printf ("%s\n","\ "String\"")//like the output in the Go source code with double quotes, use%q. Fmt. Printf ("%q\n","\ "String\"")//As with the number above, the%x output uses a base-16 encoded string, each byte represented by 2 characters. Fmt. Printf ("%x\n","Hex This")//to output A pointer value, use%p. Fmt. Printf ("%p\n", &p)//when outputting a number, you will often want to control the width and precision of the output, and you can use the number after% to control the output width. The default results use right-aligned and fill the blanks with spaces. Fmt. Printf ("|%6d|%6d|\n", A,345)//You can also specify the output width of a floating-point type, and you can also specify the precision of the output by using the syntax of the precision. Fmt. Printf ("|%6.2f|%6.2f|\n",1.2,3.45)//to be most aligned, use the-flag. Fmt. Printf ("|%-6.2f|%-6.2f|\n",1.2,3.45)//you may also want to control the width of the string output, especially to ensure that they are aligned when the class table is output. This is the basic right-aligned width representation. Fmt. Printf ("|%6s|%6s|\n","Foo","b")//to align Left, as with numbers, use the-flag. Fmt. Printf ("|%-6s|%-6s|\n","Foo","b")//so far, we've seen printf, and it's through the OS. StdOut output a formatted string. SPRINTF formats and returns a string without any output. S: = Fmt. Sprintf ("a%s","string") fmt. Println (s)//you can use fprintf to format and output to IO. Writers rather than the OS. Stdout. Fmt. fprintf (OS. Stderr,"An %s\n","Error")}
Go Language FMT Package printf method detailed
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)