In Go,fmt
The package provides a large number of formatted placeholders for formatting and outputting different types of data. Selecting the right placeholder can ensure that the output content is formatted correctly and is clear and easy to understand.
Common placeholders:
Basic Type
-
%v
: Output by default format of value. Suitable for any type. -
%+v
:exist%v
Based on the structure, the field name will be output additionally. -
%#v
: Output the value represented by Go syntax. The structure will output detailed field information and types. -
%T
: The type of output value. -
%%
: Literal percent sign (%
self).
Integer Type
-
%d
: Decimal integer. -
%b
: Binary format. -
%o
: Octal format. -
%x
: Hexadecimal format (lowercase letters). -
%X
: Hexadecimal format (capsular letters). -
%c
: Characters of the corresponding Unicode code point.
Floating point numbers and plural numbers
-
%f
: Standard floating point representation (decimal part). -
%e
: Scientific notation method (lowercase e). -
%E
: Scientific notation (caps E). -
%g
: Choose according to the specific situation%f
or%e
express.
Strings and bytes
-
%s
: String. -
%q
: String with double quotes, safe escape. -
%x
: represents a string or byte slice in hexadecimal. -
%X
: Represents a string or byte slice in capital hexadecimal.
Boolean type
-
%t
: Boolean value (true
orfalse
)。
pointer
-
%p
: Hexadecimal representation of pointer.
How to choose a placeholder:
When selecting a placeholder, you first need to select the appropriate formatting symbol according to the data type. The specific situation is as follows:
Output general data type:
use%v
It is a universal and default choice. If you are not sure about the type,%v
All types of values can be output. For example:
("Result: %v", result)
Output structure:
If you want to view the detailed information of the structure, you can use%+v
Output field names and values, or use%#v
Output the Go language format value. For example:
("Struct data: %+v", myStruct) ("Go syntax: %#v", myStruct)
Output string:
General use%s
Output string; if you need to output a safe string with double quotes and escape characters, use%q
. For example:
("User input: %s", input) ("Escaped string: %q", input)
Output integer:
use%d
Output decimal integers. If they need to be represented in binary or hexadecimal, they can be used separately.%b
and%x
. For example:
("User age: %d", age) ("Memory address in hex: %x", pointer)
Output floating point number:
General use%f
Output a floating point number, the output has a decimal part; if scientific notation is required, use%e
or%E
. For example:
("Price: %f", price) ("Price in scientific notation: %e", price)
Output pointer:
If you need to output a pointer value, use%p
Its hexadecimal address will be displayed. For example:
("Pointer address: %p", &myVar)
Actual case:
Suppose we have the following data:
taskID := 12345 price := 1234.567 name := "Alice" completed := true myStruct := struct { ID int Name string }{ ID: 1, Name: "TestTask", }
Use placeholder output:
("Task ID: %d", taskID) // Output decimal integers("Price: %f", price) // Output floating point number("User: %s", name) // Output string("Task completed: %t", completed) // Output boolean value("Task info: %+v", myStruct) // Output structure with field name and value
Placeholder list:
Placeholder | illustrate | Example |
---|---|---|
%v |
Default format | ("%v", 42) |
%+v |
Structure fields and values | ("%+v", obj) |
%#v |
Go syntax represents value | ("%#v", obj) |
%T |
Type of output value | ("%T", obj) |
%d |
Decimal integers | ("%d", 123) |
%b |
Binary integers | ("%b", 7) |
%x |
Hexadecimal lowercase | ("%x", 255) |
%f |
Floating point number | ("%f", 3.14) |
%t |
Boolean value | ("%t", true) |
%p |
Pointer address | ("%p", ptr) |
%s |
String | ("%s", "abc") |
%q |
String with double quotes | ("%q", "abc") |
By selecting the correct placeholder according to the data type, the formatting and readability of the output information can be ensured.
This is the article about the implementation example of formatted placeholders in Go language. For more related Go formatted placeholders, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!