The Printf() function can format the output using a variety of formatted verbs. Here are some common formatted verbs that can be used with all data types:
Universal formatted verb:The following verbs apply to all data types:
verb | describe |
---|---|
%v |
Print values in default format |
%#v |
Print values in Go syntax format |
%T |
Type of print value |
%% |
Print a percent sign |
Example:
package main import ( "fmt" ) func main() { var i = 15.5 var txt = "Hello World!" ("%v\n", i) ("%#v\n", i) ("%v%%\n", i) ("%T\n", i) ("%v\n", txt) ("%#v\n", txt) ("%T\n", txt) }
result:
15.5 15.5 15.5% float64 Hello World! "Hello World!" string
Integer formatting verbs
The following verbs apply to integer data types:
verb | describe |
---|---|
%b |
Binary |
%d |
Decimal |
%+d |
Signed decimal |
%o |
Octal |
%O |
Octal (with 0o prefix) |
%x |
Hexadecimal (lowercase) |
%X |
Hexadecimal (caps) |
%#x |
Hexadecimal with 0x prefix |
%4d |
Fill with spaces (width 4, right-aligned) |
%-4d |
Fill with spaces (width 4, left aligned) |
%04d |
Use zero padding (width 4) |
Example:
package main import ( "fmt" ) func main() { var i = 15 ("%b\n", i) ("%d\n", i) ("%+d\n", i) ("%o\n", i) ("%O\n", i) ("%x\n", i) ("%X\n", i) ("%#x\n", i) ("%4d\n", i) ("%-4d\n", i) ("%04d\n", i) }
result:
1111 15 +15 17 0o17 f F 0xf 15 15 0015
String formatting verbs
The following verbs are suitable for string data types:
verb | describe |
---|---|
%s |
Normal string printing |
%q |
Double quotes wrapped string print |
%8s |
Normal string printing (width 8, right-aligned) |
%-8s |
Normal string printing (width 8, left aligned) |
%x |
Hexadecimal dump of byte values |
% x |
Hexadecimal dump with spaces |
Example:
package main import ( "fmt" ) func main() { var txt = "Hello" ("%s\n", txt) ("%q\n", txt) ("%8s\n", txt) ("%-8s\n", txt) ("%x\n", txt) ("% x\n", txt) }
result:
Hello "Hello" Hello Hello 48656c6c6f 48 65 6c 6c 6f
Boolean formatted verbs
The following verbs apply to Boolean data types:
verb | describe |
---|---|
%t |
The true or false format of the boolean operator (with%v same) |
Example:
package main import ( "fmt" ) func main() { var i = true var j = false ("%t\n", i) ("%t\n", j) }
result:
true false
Floating point number formatting verb
The following verbs are suitable for floating point data types:
verb | describe |
---|---|
%e |
Scientific notation method, index is 'e' |
%f |
Decimal point, no exponential part |
%.2f |
Default width, accuracy is 2 |
%6.2f |
Width 6, accuracy 2 |
%g |
Use the index if necessary, only the necessary number of digits are retained |
Example:
package main import ( "fmt" ) func main() { var i = 3.141 ("%e\n", i) ("%f\n", i) ("%.2f\n", i) ("%6.2f\n", i) ("%g\n", i) }
result:
3.141000e+00 3.141000 3.14 3.14 3.141
Note that this translation may not be perfect, as there may be subtle differences in how it is expressed between languages.
This is the end of this article about the detailed explanation of the use of formatted verbs in Go language. For more related contents of formatted verbs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!