| 1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
/*
|
| 6 |
|
|
Package fmt implements formatted I/O with functions analogous
|
| 7 |
|
|
to C's printf and scanf. The format 'verbs' are derived from C's but
|
| 8 |
|
|
are simpler.
|
| 9 |
|
|
|
| 10 |
|
|
Printing:
|
| 11 |
|
|
|
| 12 |
|
|
The verbs:
|
| 13 |
|
|
|
| 14 |
|
|
General:
|
| 15 |
|
|
%v the value in a default format.
|
| 16 |
|
|
when printing structs, the plus flag (%+v) adds field names
|
| 17 |
|
|
%#v a Go-syntax representation of the value
|
| 18 |
|
|
%T a Go-syntax representation of the type of the value
|
| 19 |
|
|
%% a literal percent sign; consumes no value
|
| 20 |
|
|
|
| 21 |
|
|
Boolean:
|
| 22 |
|
|
%t the word true or false
|
| 23 |
|
|
Integer:
|
| 24 |
|
|
%b base 2
|
| 25 |
|
|
%c the character represented by the corresponding Unicode code point
|
| 26 |
|
|
%d base 10
|
| 27 |
|
|
%o base 8
|
| 28 |
|
|
%q a single-quoted character literal safely escaped with Go syntax.
|
| 29 |
|
|
%x base 16, with lower-case letters for a-f
|
| 30 |
|
|
%X base 16, with upper-case letters for A-F
|
| 31 |
|
|
%U Unicode format: U+1234; same as "U+%04X"
|
| 32 |
|
|
Floating-point and complex constituents:
|
| 33 |
|
|
%b decimalless scientific notation with exponent a power of two,
|
| 34 |
|
|
in the manner of strconv.FormatFloat with the 'b' format,
|
| 35 |
|
|
e.g. -123456p-78
|
| 36 |
|
|
%e scientific notation, e.g. -1234.456e+78
|
| 37 |
|
|
%E scientific notation, e.g. -1234.456E+78
|
| 38 |
|
|
%f decimal point but no exponent, e.g. 123.456
|
| 39 |
|
|
%g whichever of %e or %f produces more compact output
|
| 40 |
|
|
%G whichever of %E or %f produces more compact output
|
| 41 |
|
|
String and slice of bytes:
|
| 42 |
|
|
%s the uninterpreted bytes of the string or slice
|
| 43 |
|
|
%q a double-quoted string safely escaped with Go syntax
|
| 44 |
|
|
%x base 16, lower-case, two characters per byte
|
| 45 |
|
|
%X base 16, upper-case, two characters per byte
|
| 46 |
|
|
Pointer:
|
| 47 |
|
|
%p base 16 notation, with leading 0x
|
| 48 |
|
|
|
| 49 |
|
|
There is no 'u' flag. Integers are printed unsigned if they have unsigned type.
|
| 50 |
|
|
Similarly, there is no need to specify the size of the operand (int8, int64).
|
| 51 |
|
|
|
| 52 |
|
|
The width and precision control formatting and are in units of Unicode
|
| 53 |
|
|
code points. (This differs from C's printf where the units are numbers
|
| 54 |
|
|
of bytes.) Either or both of the flags may be replaced with the
|
| 55 |
|
|
character '*', causing their values to be obtained from the next
|
| 56 |
|
|
operand, which must be of type int.
|
| 57 |
|
|
|
| 58 |
|
|
For numeric values, width sets the width of the field and precision
|
| 59 |
|
|
sets the number of places after the decimal, if appropriate. For
|
| 60 |
|
|
example, the format %6.2f prints 123.45.
|
| 61 |
|
|
|
| 62 |
|
|
For strings, width is the minimum number of characters to output,
|
| 63 |
|
|
padding with spaces if necessary, and precision is the maximum
|
| 64 |
|
|
number of characters to output, truncating if necessary.
|
| 65 |
|
|
|
| 66 |
|
|
Other flags:
|
| 67 |
|
|
+ always print a sign for numeric values;
|
| 68 |
|
|
guarantee ASCII-only output for %q (%+q)
|
| 69 |
|
|
- pad with spaces on the right rather than the left (left-justify the field)
|
| 70 |
|
|
# alternate format: add leading 0 for octal (%#o), 0x for hex (%#x);
|
| 71 |
|
|
0X for hex (%#X); suppress 0x for %p (%#p);
|
| 72 |
|
|
print a raw (backquoted) string if possible for %q (%#q);
|
| 73 |
|
|
write e.g. U+0078 'x' if the character is printable for %U (%#U).
|
| 74 |
|
|
' ' (space) leave a space for elided sign in numbers (% d);
|
| 75 |
|
|
put spaces between bytes printing strings or slices in hex (% x, % X)
|
| 76 |
|
|
|
| 77 |
|
|
|
| 78 |
|
|
For each Printf-like function, there is also a Print function
|
| 79 |
|
|
that takes no format and is equivalent to saying %v for every
|
| 80 |
|
|
operand. Another variant Println inserts blanks between
|
| 81 |
|
|
operands and appends a newline.
|
| 82 |
|
|
|
| 83 |
|
|
Regardless of the verb, if an operand is an interface value,
|
| 84 |
|
|
the internal concrete value is used, not the interface itself.
|
| 85 |
|
|
Thus:
|
| 86 |
|
|
var i interface{} = 23
|
| 87 |
|
|
fmt.Printf("%v\n", i)
|
| 88 |
|
|
will print 23.
|
| 89 |
|
|
|
| 90 |
|
|
If an operand implements interface Formatter, that interface
|
| 91 |
|
|
can be used for fine control of formatting.
|
| 92 |
|
|
|
| 93 |
|
|
If the format (which is implicitly %v for Println etc.) is valid
|
| 94 |
|
|
for a string (%s %q %v %x %X), the following two rules also apply:
|
| 95 |
|
|
|
| 96 |
|
|
1. If an operand implements the error interface, the Error method
|
| 97 |
|
|
will be used to convert the object to a string, which will then
|
| 98 |
|
|
be formatted as required by the verb (if any).
|
| 99 |
|
|
|
| 100 |
|
|
2. If an operand implements method String() string, that method
|
| 101 |
|
|
will be used to convert the object to a string, which will then
|
| 102 |
|
|
be formatted as required by the verb (if any).
|
| 103 |
|
|
|
| 104 |
|
|
To avoid recursion in cases such as
|
| 105 |
|
|
type X string
|
| 106 |
|
|
func (x X) String() string { return Sprintf("<%s>", x) }
|
| 107 |
|
|
convert the value before recurring:
|
| 108 |
|
|
func (x X) String() string { return Sprintf("<%s>", string(x)) }
|
| 109 |
|
|
|
| 110 |
|
|
Format errors:
|
| 111 |
|
|
|
| 112 |
|
|
If an invalid argument is given for a verb, such as providing
|
| 113 |
|
|
a string to %d, the generated string will contain a
|
| 114 |
|
|
description of the problem, as in these examples:
|
| 115 |
|
|
|
| 116 |
|
|
Wrong type or unknown verb: %!verb(type=value)
|
| 117 |
|
|
Printf("%d", hi): %!d(string=hi)
|
| 118 |
|
|
Too many arguments: %!(EXTRA type=value)
|
| 119 |
|
|
Printf("hi", "guys"): hi%!(EXTRA string=guys)
|
| 120 |
|
|
Too few arguments: %!verb(MISSING)
|
| 121 |
|
|
Printf("hi%d"): hi %!d(MISSING)
|
| 122 |
|
|
Non-int for width or precision: %!(BADWIDTH) or %!(BADPREC)
|
| 123 |
|
|
Printf("%*s", 4.5, "hi"): %!(BADWIDTH)hi
|
| 124 |
|
|
Printf("%.*s", 4.5, "hi"): %!(BADPREC)hi
|
| 125 |
|
|
|
| 126 |
|
|
All errors begin with the string "%!" followed sometimes
|
| 127 |
|
|
by a single character (the verb) and end with a parenthesized
|
| 128 |
|
|
description.
|
| 129 |
|
|
|
| 130 |
|
|
Scanning:
|
| 131 |
|
|
|
| 132 |
|
|
An analogous set of functions scans formatted text to yield
|
| 133 |
|
|
values. Scan, Scanf and Scanln read from os.Stdin; Fscan,
|
| 134 |
|
|
Fscanf and Fscanln read from a specified io.Reader; Sscan,
|
| 135 |
|
|
Sscanf and Sscanln read from an argument string. Scanln,
|
| 136 |
|
|
Fscanln and Sscanln stop scanning at a newline and require that
|
| 137 |
|
|
the items be followed by one; Sscanf, Fscanf and Sscanf require
|
| 138 |
|
|
newlines in the input to match newlines in the format; the other
|
| 139 |
|
|
routines treat newlines as spaces.
|
| 140 |
|
|
|
| 141 |
|
|
Scanf, Fscanf, and Sscanf parse the arguments according to a
|
| 142 |
|
|
format string, analogous to that of Printf. For example, %x
|
| 143 |
|
|
will scan an integer as a hexadecimal number, and %v will scan
|
| 144 |
|
|
the default representation format for the value.
|
| 145 |
|
|
|
| 146 |
|
|
The formats behave analogously to those of Printf with the
|
| 147 |
|
|
following exceptions:
|
| 148 |
|
|
|
| 149 |
|
|
%p is not implemented
|
| 150 |
|
|
%T is not implemented
|
| 151 |
|
|
%e %E %f %F %g %G are all equivalent and scan any floating point or complex value
|
| 152 |
|
|
%s and %v on strings scan a space-delimited token
|
| 153 |
|
|
|
| 154 |
|
|
The familiar base-setting prefixes 0 (octal) and 0x
|
| 155 |
|
|
(hexadecimal) are accepted when scanning integers without a
|
| 156 |
|
|
format or with the %v verb.
|
| 157 |
|
|
|
| 158 |
|
|
Width is interpreted in the input text (%5s means at most
|
| 159 |
|
|
five runes of input will be read to scan a string) but there
|
| 160 |
|
|
is no syntax for scanning with a precision (no %5.2f, just
|
| 161 |
|
|
%5f).
|
| 162 |
|
|
|
| 163 |
|
|
When scanning with a format, all non-empty runs of space
|
| 164 |
|
|
characters (except newline) are equivalent to a single
|
| 165 |
|
|
space in both the format and the input. With that proviso,
|
| 166 |
|
|
text in the format string must match the input text; scanning
|
| 167 |
|
|
stops if it does not, with the return value of the function
|
| 168 |
|
|
indicating the number of arguments scanned.
|
| 169 |
|
|
|
| 170 |
|
|
In all the scanning functions, if an operand implements method
|
| 171 |
|
|
Scan (that is, it implements the Scanner interface) that
|
| 172 |
|
|
method will be used to scan the text for that operand. Also,
|
| 173 |
|
|
if the number of arguments scanned is less than the number of
|
| 174 |
|
|
arguments provided, an error is returned.
|
| 175 |
|
|
|
| 176 |
|
|
All arguments to be scanned must be either pointers to basic
|
| 177 |
|
|
types or implementations of the Scanner interface.
|
| 178 |
|
|
|
| 179 |
|
|
Note: Fscan etc. can read one character (rune) past the input
|
| 180 |
|
|
they return, which means that a loop calling a scan routine
|
| 181 |
|
|
may skip some of the input. This is usually a problem only
|
| 182 |
|
|
when there is no space between input values. If the reader
|
| 183 |
|
|
provided to Fscan implements ReadRune, that method will be used
|
| 184 |
|
|
to read characters. If the reader also implements UnreadRune,
|
| 185 |
|
|
that method will be used to save the character and successive
|
| 186 |
|
|
calls will not lose data. To attach ReadRune and UnreadRune
|
| 187 |
|
|
methods to a reader without that capability, use
|
| 188 |
|
|
bufio.NewReader.
|
| 189 |
|
|
*/
|
| 190 |
|
|
package fmt
|