37
37
%e scientific notation, e.g. -1234.456e+78
38
38
%E scientific notation, e.g. -1234.456E+78
39
39
%f decimal point but no exponent, e.g. 123.456
40
41
%g whichever of %e or %f produces more compact output
41
42
%G whichever of %E or %f produces more compact output
42
43
String and slice of bytes:
50
51
There is no 'u' flag. Integers are printed unsigned if they have unsigned type.
51
52
Similarly, there is no need to specify the size of the operand (int8, int64).
53
The width and precision control formatting and are in units of Unicode
54
code points. (This differs from C's printf where the units are numbers
54
Width is specified by an optional decimal number immediately following the verb.
55
If absent, the width is whatever is necessary to represent the value.
56
Precision is specified after the (optional) width by a period followed by a
57
decimal number. If no period is present, a default precision is used.
58
A period with no following number specifies a precision of zero.
60
%f: default width, default precision
61
%9f width 9, default precision
62
%.2f default width, precision 2
63
%9.2f width 9, precision 2
64
%9.f width 9, precision 0
66
Width and precision are measured in units of Unicode code points.
67
(This differs from C's printf where the units are numbers
55
68
of bytes.) Either or both of the flags may be replaced with the
56
69
character '*', causing their values to be obtained from the next
57
70
operand, which must be of type int.
59
For numeric values, width sets the minimum width of the field and
72
For most values, width is the minimum number of characters to output,
73
padding the formatted form with spaces if necessary.
74
For strings, precision is the maximum number of characters to output,
75
truncating if necessary.
77
For floating-point values, width sets the minimum width of the field and
60
78
precision sets the number of places after the decimal, if appropriate,
61
79
except that for %g/%G it sets the total number of digits. For example,
62
80
given 123.45 the format %6.2f prints 123.45 while %.4g prints 123.5.
63
81
The default precision for %e and %f is 6; for %g it is the smallest
64
82
number of digits necessary to identify the value uniquely.
66
For most values, width is the minimum number of characters to output,
67
padding the formatted form with spaces if necessary.
68
For strings, precision is the maximum number of characters to output,
69
truncating if necessary.
84
For complex numbers, the width and precision apply to the two
85
components independently and the result is parenthesized, so %f applied
86
to 1.2+3.4i produces (1.200000+3.400000i).
72
89
+ always print a sign for numeric values;
98
115
fmt.Printf("%v\n", i)
101
If an operand implements interface Formatter, that interface
102
can be used for fine control of formatting.
118
Except when printed using the verbs %T and %p, special
119
formatting considerations apply for operands that implement
120
certain interfaces. In order of application:
122
1. If an operand implements the Formatter interface, it will
123
be invoked. Formatter provides fine control of formatting.
125
2. If the %v verb is used with the # flag (%#v) and the operand
126
implements the GoStringer interface, that will be invoked.
104
128
If the format (which is implicitly %v for Println etc.) is valid
105
for a string (%s %q %v %x %X), the following two rules also apply:
107
1. If an operand implements the error interface, the Error method
108
will be used to convert the object to a string, which will then
109
be formatted as required by the verb (if any).
111
2. If an operand implements method String() string, that method
112
will be used to convert the object to a string, which will then
113
be formatted as required by the verb (if any).
129
for a string (%s %q %v %x %X), the following two rules apply:
131
3. If an operand implements the error interface, the Error method
132
will be invoked to convert the object to a string, which will then
133
be formatted as required by the verb (if any).
135
4. If an operand implements method String() string, that method
136
will be invoked to convert the object to a string, which will then
137
be formatted as required by the verb (if any).
139
For compound operands such as slices and structs, the format
140
applies to the elements of each operand, recursively, not to the
141
operand as a whole. Thus %q will quote each element of a slice
142
of strings, and %6.2f will control formatting for each element
143
of a floating-point array.
115
145
To avoid recursion in cases such as