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

View as plain text