Source file src/fmt/doc.go
1 // 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 There are four families of printing functions defined by their output destination. 13 [Print], [Println] and [Printf] write to [os.Stdout]; 14 [Sprint], [Sprintln] and [Sprintf] return a string; 15 [Fprint], [Fprintln] and [Fprintf] write to an [io.Writer]; and 16 [Append], [Appendln] and [Appendf] append the output to a byte slice. 17 18 The functions within each family do the formatting according to the end of the name. 19 Print, Sprint, Fprint and Append use the default format for each argument, 20 adding a space between operands when neither is a string. 21 Println, Sprintln, Fprintln and Appendln always add spaces and append a newline. 22 Printf, Sprintf, Fprintf and Appendf use a sequence of "verbs" to control the formatting. 23 24 The verbs: 25 26 General: 27 28 %v the value in a default format 29 when printing structs, the plus flag (%+v) adds field names 30 %#v a Go-syntax representation of the value 31 (floating-point infinities and NaNs print as ±Inf and NaN) 32 %T a Go-syntax representation of the type of the value 33 %% a literal percent sign; consumes no value 34 35 Boolean: 36 37 %t the word true or false 38 39 Integer: 40 41 %b base 2 42 %c the character represented by the corresponding Unicode code point 43 %d base 10 44 %o base 8 45 %O base 8 with 0o prefix 46 %q a single-quoted rune literal safely escaped with Go syntax. 47 %x base 16, with lower-case letters for a-f 48 %X base 16, with upper-case letters for A-F 49 %U Unicode format: U+1234; same as "U+%04X" 50 51 Floating-point and complex constituents: 52 53 %b decimalless scientific notation with exponent a power of two, 54 in the manner of strconv.FormatFloat with the 'b' format, 55 e.g. -123456p-78 56 %e scientific notation, e.g. -1.234456e+78 57 %E scientific notation, e.g. -1.234456E+78 58 %f decimal point but no exponent, e.g. 123.456 59 %F synonym for %f 60 %g %e for large exponents, %f otherwise. Precision is discussed below. 61 %G %E for large exponents, %F otherwise 62 %x hexadecimal notation (with decimal power of two exponent), e.g. -0x1.23abcp+20 63 %X upper-case hexadecimal notation, e.g. -0X1.23ABCP+20 64 65 The exponent is always a decimal integer. 66 For formats other than %b the exponent is at least two digits. 67 68 String and slice of bytes (treated equivalently with these verbs): 69 70 %s the uninterpreted bytes of the string or slice 71 %q a double-quoted string safely escaped with Go syntax 72 %x base 16, lower-case, two characters per byte 73 %X base 16, upper-case, two characters per byte 74 75 Slice: 76 77 %p address of 0th element in base 16 notation, with leading 0x 78 79 Pointer: 80 81 %p base 16 notation, with leading 0x 82 The %b, %d, %o, %x and %X verbs also work with pointers, 83 formatting the value exactly as if it were an integer. 84 85 The default format for %v is: 86 87 bool: %t 88 int, int8 etc.: %d 89 uint, uint8 etc.: %d, %#x if printed with %#v 90 float32, complex64, etc: %g 91 string: %s 92 chan: %p 93 pointer: %p 94 95 For compound objects, the elements are printed using these rules, recursively, 96 laid out like this: 97 98 struct: {field0 field1 ...} 99 array, slice: [elem0 elem1 ...] 100 maps: map[key1:value1 key2:value2 ...] 101 pointer to above: &{}, &[], &map[] 102 103 Width is specified by an optional decimal number immediately preceding the verb. 104 If absent, the width is whatever is necessary to represent the value. 105 Precision is specified after the (optional) width by a period followed by a 106 decimal number. If no period is present, a default precision is used. 107 A period with no following number specifies a precision of zero. 108 Examples: 109 110 %f default width, default precision 111 %9f width 9, default precision 112 %.2f default width, precision 2 113 %9.2f width 9, precision 2 114 %9.f width 9, precision 0 115 116 Width and precision are measured in units of Unicode code points, 117 that is, runes. (This differs from C's printf where the 118 units are always measured in bytes.) Either or both of the flags 119 may be replaced with the character '*', causing their values to be 120 obtained from the next operand (preceding the one to format), 121 which must be of type int. 122 123 For most values, width is the minimum number of runes to output, 124 padding the formatted form with spaces if necessary. 125 126 For strings, byte slices and byte arrays, however, precision 127 limits the length of the input to be formatted (not the size of 128 the output), truncating if necessary. Normally it is measured in 129 runes, but for these types when formatted with the %x or %X format 130 it is measured in bytes. 131 132 For floating-point values, width sets the minimum width of the field and 133 precision sets the number of places after the decimal, if appropriate, 134 except that for %g/%G precision sets the maximum number of significant 135 digits (trailing zeros are removed). For example, given 12.345 the format 136 %6.3f prints 12.345 while %.3g prints 12.3. The default precision for %e, %f 137 and %#g is 6; for %g it is the smallest number of digits necessary to identify 138 the value uniquely. 139 140 For complex numbers, the width and precision apply to the two 141 components independently and the result is parenthesized, so %f applied 142 to 1.2+3.4i produces (1.200000+3.400000i). 143 144 When formatting a single integer code point or a rune string (type []rune) 145 with %q, invalid Unicode code points are changed to the Unicode replacement 146 character, U+FFFD, as in [strconv.QuoteRune]. 147 148 Other flags: 149 150 '+' always print a sign for numeric values; 151 guarantee ASCII-only output for %q (%+q) 152 '-' pad with spaces on the right rather than the left (left-justify the field) 153 '#' alternate format: add leading 0b for binary (%#b), 0 for octal (%#o), 154 0x or 0X for hex (%#x or %#X); suppress 0x for %p (%#p); 155 for %q, print a raw (backquoted) string if [strconv.CanBackquote] 156 returns true; 157 always print a decimal point for %e, %E, %f, %F, %g and %G; 158 do not remove trailing zeros for %g and %G; 159 write e.g. U+0078 'x' if the character is printable for %U (%#U) 160 ' ' (space) leave a space for elided sign in numbers (% d); 161 put spaces between bytes printing strings or slices in hex (% x, % X) 162 '0' pad with leading zeros rather than spaces; 163 for numbers, this moves the padding after the sign 164 165 Flags are ignored by verbs that do not expect them. 166 For example there is no alternate decimal format, so %#d and %d 167 behave identically. 168 169 For each Printf-like function, there is also a Print function 170 that takes no format and is equivalent to saying %v for every 171 operand. Another variant Println inserts blanks between 172 operands and appends a newline. 173 174 Regardless of the verb, if an operand is an interface value, 175 the internal concrete value is used, not the interface itself. 176 Thus: 177 178 var i interface{} = 23 179 fmt.Printf("%v\n", i) 180 181 will print 23. 182 183 Except when printed using the verbs %T and %p, special 184 formatting considerations apply for operands that implement 185 certain interfaces. In order of application: 186 187 1. If the operand is a [reflect.Value], the operand is replaced by the 188 concrete value that it holds, and printing continues with the next rule. 189 190 2. If an operand implements the [Formatter] interface, it will 191 be invoked. In this case the interpretation of verbs and flags is 192 controlled by that implementation. 193 194 3. If the %v verb is used with the # flag (%#v) and the operand 195 implements the [GoStringer] interface, that will be invoked. 196 197 If the format (which is implicitly %v for [Println] etc.) is valid 198 for a string (%s %q %x %X), or is %v but not %#v, 199 the following two rules apply: 200 201 4. If an operand implements the error interface, the Error method 202 will be invoked to convert the object to a string, which will then 203 be formatted as required by the verb (if any). 204 205 5. If an operand implements method String() string, that method 206 will be invoked to convert the object to a string, which will then 207 be formatted as required by the verb (if any). 208 209 For compound operands such as slices and structs, the format 210 applies to the elements of each operand, recursively, not to the 211 operand as a whole. Thus %q will quote each element of a slice 212 of strings, and %6.2f will control formatting for each element 213 of a floating-point array. 214 215 However, when printing a byte slice with a string-like verb 216 (%s %q %x %X), it is treated identically to a string, as a single item. 217 218 To avoid recursion in cases such as 219 220 type X string 221 func (x X) String() string { return Sprintf("<%s>", x) } 222 223 convert the value before recurring: 224 225 func (x X) String() string { return Sprintf("<%s>", string(x)) } 226 227 Infinite recursion can also be triggered by self-referential data 228 structures, such as a slice that contains itself as an element, if 229 that type has a String method. Such pathologies are rare, however, 230 and the package does not protect against them. 231 232 When printing a struct, fmt cannot and therefore does not invoke 233 formatting methods such as Error or String on unexported fields. 234 235 # Explicit argument indexes 236 237 In [Printf], [Sprintf], [Fprintf], and [Appendf], the default behavior is for each 238 formatting verb to format successive arguments passed in the call. 239 However, the notation [n] immediately before the verb indicates that the 240 nth one-indexed argument is to be formatted instead. The same notation 241 before a '*' for a width or precision selects the argument index holding 242 the value. After processing a bracketed expression [n], subsequent verbs 243 will use arguments n+1, n+2, etc. unless otherwise directed. 244 245 For example, 246 247 fmt.Sprintf("%[2]d %[1]d\n", 11, 22) 248 249 will yield "22 11", while 250 251 fmt.Sprintf("%[3]*.[2]*[1]f", 12.0, 2, 6) 252 253 equivalent to 254 255 fmt.Sprintf("%6.2f", 12.0) 256 257 will yield " 12.00". Because an explicit index affects subsequent verbs, 258 this notation can be used to print the same values multiple times 259 by resetting the index for the first argument to be repeated: 260 261 fmt.Sprintf("%d %d %#[1]x %#x", 16, 17) 262 263 will yield "16 17 0x10 0x11". 264 265 # Format errors 266 267 If an invalid argument is given for a verb, such as providing 268 a string to %d, the generated string will contain a 269 description of the problem, as in these examples: 270 271 Wrong type or unknown verb: %!verb(type=value) 272 Printf("%d", "hi"): %!d(string=hi) 273 Too many arguments: %!(EXTRA type=value) 274 Printf("hi", "guys"): hi%!(EXTRA string=guys) 275 Too few arguments: %!verb(MISSING) 276 Printf("hi%d"): hi%!d(MISSING) 277 Non-int for width or precision: %!(BADWIDTH) or %!(BADPREC) 278 Printf("%*s", 4.5, "hi"): %!(BADWIDTH)hi 279 Printf("%.*s", 4.5, "hi"): %!(BADPREC)hi 280 Invalid or invalid use of argument index: %!(BADINDEX) 281 Printf("%*[2]d", 7): %!d(BADINDEX) 282 Printf("%.[2]d", 7): %!d(BADINDEX) 283 284 All errors begin with the string "%!" followed sometimes 285 by a single character (the verb) and end with a parenthesized 286 description. 287 288 If an Error or String method triggers a panic when called by a 289 print routine, the fmt package reformats the error message 290 from the panic, decorating it with an indication that it came 291 through the fmt package. For example, if a String method 292 calls panic("bad"), the resulting formatted message will look 293 like 294 295 %!s(PANIC=bad) 296 297 The %!s just shows the print verb in use when the failure 298 occurred. If the panic is caused by a nil receiver to an Error, 299 String, or GoString method, however, the output is the undecorated 300 string, "<nil>". 301 302 Many packages whose APIs involve string formatting expose a trio of 303 functions similar to [Print], [Printf], and [Println]. 304 For example, the [log.Print], [log.Printf] and [log.Println] functions 305 forward their arguments to the corresponding functions in this package. 306 To avoid confusion, other wrapper functions should follow the naming 307 and behavioral conventions established by this package. 308 In particular, a single function should not choose between literal 309 printing (like [Print]) and formatted printing (like [Printf]) 310 based on the number of arguments; instead, provide separate functions. 311 312 # Scanning 313 314 An analogous set of functions scans formatted text to yield 315 values. [Scan], [Scanf] and [Scanln] read from [os.Stdin]; [Fscan], 316 [Fscanf] and [Fscanln] read from a specified [io.Reader]; [Sscan], 317 [Sscanf] and [Sscanln] read from an argument string. 318 319 [Scan], [Fscan], [Sscan] treat newlines in the input as spaces. 320 321 [Scanln], [Fscanln] and [Sscanln] stop scanning at a newline and 322 require that the items be followed by a newline or EOF. 323 324 [Scanf], [Fscanf], and [Sscanf] parse the arguments according to a 325 format string, analogous to that of [Printf]. In the text that 326 follows, 'space' means any Unicode whitespace character 327 except newline. 328 329 In the format string, a verb introduced by the % character 330 consumes and parses input; these verbs are described in more 331 detail below. A character other than %, space, or newline in 332 the format consumes exactly that input character, which must 333 be present. A newline with zero or more spaces before it in 334 the format string consumes zero or more spaces in the input 335 followed by a single newline or the end of the input. A space 336 following a newline in the format string consumes zero or more 337 spaces in the input. Otherwise, any run of one or more spaces 338 in the format string consumes as many spaces as possible in 339 the input. Unless the run of spaces in the format string 340 appears adjacent to a newline, the run must consume at least 341 one space from the input or find the end of the input. 342 343 The handling of spaces and newlines differs from that of C's 344 scanf family: in C, newlines are treated as any other space, 345 and it is never an error when a run of spaces in the format 346 string finds no spaces to consume in the input. 347 348 The verbs behave analogously to those of [Printf]. 349 For example, %x will scan an integer as a hexadecimal number, 350 and %v will scan the default representation format for the value. 351 The [Printf] verbs %p and %T and the flags # and + are not implemented. 352 For floating-point and complex values, all valid formatting verbs 353 (%b %e %E %f %F %g %G %x %X and %v) are equivalent and accept 354 both decimal and hexadecimal notation (for example: "2.3e+7", "0x4.5p-8") 355 and digit-separating underscores (for example: "3.14159_26535_89793"). 356 357 Input processed by verbs is implicitly space-delimited: the 358 implementation of every verb except %c starts by discarding 359 leading spaces from the remaining input, and the %s verb 360 (and %v reading into a string) stops consuming input at the first 361 space or newline character. 362 363 The familiar base-setting prefixes 0b (binary), 0o and 0 (octal), 364 and 0x (hexadecimal) are accepted when scanning integers 365 without a format or with the %v verb, as are digit-separating 366 underscores. 367 368 Width is interpreted in the input text but there is no 369 syntax for scanning with a precision (no %5.2f, just %5f). 370 If width is provided, it applies after leading spaces are 371 trimmed and specifies the maximum number of runes to read 372 to satisfy the verb. For example, 373 374 Sscanf(" 1234567 ", "%5s%d", &s, &i) 375 376 will set s to "12345" and i to 67 while 377 378 Sscanf(" 12 34 567 ", "%5s%d", &s, &i) 379 380 will set s to "12" and i to 34. 381 382 In all the scanning functions, a carriage return followed 383 immediately by a newline is treated as a plain newline 384 (\r\n means the same as \n). 385 386 In all the scanning functions, if an operand implements method 387 [Scan] (that is, it implements the [Scanner] interface) that 388 method will be used to scan the text for that operand. Also, 389 if the number of arguments scanned is less than the number of 390 arguments provided, an error is returned. 391 392 All arguments to be scanned must be either pointers to basic 393 types or implementations of the [Scanner] interface. 394 395 Like [Scanf] and [Fscanf], [Sscanf] need not consume its entire input. 396 There is no way to recover how much of the input string [Sscanf] used. 397 398 Note: [Fscan] etc. can read one character (rune) past the input 399 they return, which means that a loop calling a scan routine 400 may skip some of the input. This is usually a problem only 401 when there is no space between input values. If the reader 402 provided to [Fscan] implements ReadRune, that method will be used 403 to read characters. If the reader also implements UnreadRune, 404 that method will be used to save the character and successive 405 calls will not lose data. To attach ReadRune and UnreadRune 406 methods to a reader without that capability, use 407 [bufio.NewReader]. 408 */ 409 package fmt 410