You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1148 lines
38 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>fmt - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package fmt</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "fmt"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. </dl>
  58. </div>
  59. <!-- The package's Name is printed as title by the top-level template -->
  60. <div id="pkg-overview" class="toggleVisible">
  61. <div class="collapsed">
  62. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  63. </div>
  64. <div class="expanded">
  65. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  66. <p>
  67. Package fmt implements formatted I/O with functions analogous
  68. to C&#39;s printf and scanf. The format &#39;verbs&#39; are derived from C&#39;s but
  69. are simpler.
  70. </p>
  71. <h3 id="hdr-Printing">Printing</h3>
  72. <p>
  73. The verbs:
  74. </p>
  75. <p>
  76. General:
  77. </p>
  78. <pre>%v the value in a default format
  79. when printing structs, the plus flag (%+v) adds field names
  80. %#v a Go-syntax representation of the value
  81. %T a Go-syntax representation of the type of the value
  82. %% a literal percent sign; consumes no value
  83. </pre>
  84. <p>
  85. Boolean:
  86. </p>
  87. <pre>%t the word true or false
  88. </pre>
  89. <p>
  90. Integer:
  91. </p>
  92. <pre>%b base 2
  93. %c the character represented by the corresponding Unicode code point
  94. %d base 10
  95. %o base 8
  96. %q a single-quoted character literal safely escaped with Go syntax.
  97. %x base 16, with lower-case letters for a-f
  98. %X base 16, with upper-case letters for A-F
  99. %U Unicode format: U+1234; same as &#34;U+%04X&#34;
  100. </pre>
  101. <p>
  102. Floating-point and complex constituents:
  103. </p>
  104. <pre>%b decimalless scientific notation with exponent a power of two,
  105. in the manner of strconv.FormatFloat with the &#39;b&#39; format,
  106. e.g. -123456p-78
  107. %e scientific notation, e.g. -1.234456e+78
  108. %E scientific notation, e.g. -1.234456E+78
  109. %f decimal point but no exponent, e.g. 123.456
  110. %F synonym for %f
  111. %g %e for large exponents, %f otherwise
  112. %G %E for large exponents, %F otherwise
  113. </pre>
  114. <p>
  115. String and slice of bytes (treated equivalently with these verbs):
  116. </p>
  117. <pre>%s the uninterpreted bytes of the string or slice
  118. %q a double-quoted string safely escaped with Go syntax
  119. %x base 16, lower-case, two characters per byte
  120. %X base 16, upper-case, two characters per byte
  121. </pre>
  122. <p>
  123. Pointer:
  124. </p>
  125. <pre>%p base 16 notation, with leading 0x
  126. </pre>
  127. <p>
  128. There is no &#39;u&#39; flag. Integers are printed unsigned if they have unsigned type.
  129. Similarly, there is no need to specify the size of the operand (int8, int64).
  130. </p>
  131. <p>
  132. The default format for %v is:
  133. </p>
  134. <pre>bool: %t
  135. int, int8 etc.: %d
  136. uint, uint8 etc.: %d, %x if printed with %#v
  137. float32, complex64, etc: %g
  138. string: %s
  139. chan: %p
  140. pointer: %p
  141. </pre>
  142. <p>
  143. For compound objects, the elements are printed using these rules, recursively,
  144. laid out like this:
  145. </p>
  146. <pre>struct: {field0 field1 ...}
  147. array, slice: [elem0 elem1 ...]
  148. maps: map[key1:value1 key2:value2]
  149. pointer to above: &amp;{}, &amp;[], &amp;map[]
  150. </pre>
  151. <p>
  152. Width is specified by an optional decimal number immediately preceding the verb.
  153. If absent, the width is whatever is necessary to represent the value.
  154. Precision is specified after the (optional) width by a period followed by a
  155. decimal number. If no period is present, a default precision is used.
  156. A period with no following number specifies a precision of zero.
  157. Examples:
  158. </p>
  159. <pre>%f default width, default precision
  160. %9f width 9, default precision
  161. %.2f default width, precision 2
  162. %9.2f width 9, precision 2
  163. %9.f width 9, precision 0
  164. </pre>
  165. <p>
  166. Width and precision are measured in units of Unicode code points,
  167. that is, runes. (This differs from C&#39;s printf where the
  168. units are always measured in bytes.) Either or both of the flags
  169. may be replaced with the character &#39;*&#39;, causing their values to be
  170. obtained from the next operand, which must be of type int.
  171. </p>
  172. <p>
  173. For most values, width is the minimum number of runes to output,
  174. padding the formatted form with spaces if necessary.
  175. </p>
  176. <p>
  177. For strings, byte slices and byte arrays, however, precision
  178. limits the length of the input to be formatted (not the size of
  179. the output), truncating if necessary. Normally it is measured in
  180. runes, but for these types when formatted with the %x or %X format
  181. it is measured in bytes.
  182. </p>
  183. <p>
  184. For floating-point values, width sets the minimum width of the field and
  185. precision sets the number of places after the decimal, if appropriate,
  186. except that for %g/%G it sets the total number of digits. For example,
  187. given 123.45 the format %6.2f prints 123.45 while %.4g prints 123.5.
  188. The default precision for %e and %f is 6; for %g it is the smallest
  189. number of digits necessary to identify the value uniquely.
  190. </p>
  191. <p>
  192. For complex numbers, the width and precision apply to the two
  193. components independently and the result is parenthesized, so %f applied
  194. to 1.2+3.4i produces (1.200000+3.400000i).
  195. </p>
  196. <p>
  197. Other flags:
  198. </p>
  199. <pre>+ always print a sign for numeric values;
  200. guarantee ASCII-only output for %q (%+q)
  201. - pad with spaces on the right rather than the left (left-justify the field)
  202. # alternate format: add leading 0 for octal (%#o), 0x for hex (%#x);
  203. 0X for hex (%#X); suppress 0x for %p (%#p);
  204. for %q, print a raw (backquoted) string if strconv.CanBackquote
  205. returns true;
  206. write e.g. U+0078 &#39;x&#39; if the character is printable for %U (%#U).
  207. &#39; &#39; (space) leave a space for elided sign in numbers (% d);
  208. put spaces between bytes printing strings or slices in hex (% x, % X)
  209. 0 pad with leading zeros rather than spaces;
  210. for numbers, this moves the padding after the sign
  211. </pre>
  212. <p>
  213. Flags are ignored by verbs that do not expect them.
  214. For example there is no alternate decimal format, so %#d and %d
  215. behave identically.
  216. </p>
  217. <p>
  218. For each Printf-like function, there is also a Print function
  219. that takes no format and is equivalent to saying %v for every
  220. operand. Another variant Println inserts blanks between
  221. operands and appends a newline.
  222. </p>
  223. <p>
  224. Regardless of the verb, if an operand is an interface value,
  225. the internal concrete value is used, not the interface itself.
  226. Thus:
  227. </p>
  228. <pre>var i interface{} = 23
  229. fmt.Printf(&#34;%v\n&#34;, i)
  230. </pre>
  231. <p>
  232. will print 23.
  233. </p>
  234. <p>
  235. Except when printed using the verbs %T and %p, special
  236. formatting considerations apply for operands that implement
  237. certain interfaces. In order of application:
  238. </p>
  239. <p>
  240. 1. If the operand is a reflect.Value, the operand is replaced by the
  241. concrete value that it holds, and printing continues with the next rule.
  242. </p>
  243. <p>
  244. 2. If an operand implements the Formatter interface, it will
  245. be invoked. Formatter provides fine control of formatting.
  246. </p>
  247. <p>
  248. 3. If the %v verb is used with the # flag (%#v) and the operand
  249. implements the GoStringer interface, that will be invoked.
  250. </p>
  251. <p>
  252. If the format (which is implicitly %v for Println etc.) is valid
  253. for a string (%s %q %v %x %X), the following two rules apply:
  254. </p>
  255. <p>
  256. 4. If an operand implements the error interface, the Error method
  257. will be invoked to convert the object to a string, which will then
  258. be formatted as required by the verb (if any).
  259. </p>
  260. <p>
  261. 5. If an operand implements method String() string, that method
  262. will be invoked to convert the object to a string, which will then
  263. be formatted as required by the verb (if any).
  264. </p>
  265. <p>
  266. For compound operands such as slices and structs, the format
  267. applies to the elements of each operand, recursively, not to the
  268. operand as a whole. Thus %q will quote each element of a slice
  269. of strings, and %6.2f will control formatting for each element
  270. of a floating-point array.
  271. </p>
  272. <p>
  273. However, when printing a byte slice with a string-like verb
  274. (%s %q %x %X), it is treated identically to a string, as a single item.
  275. </p>
  276. <p>
  277. To avoid recursion in cases such as
  278. </p>
  279. <pre>type X string
  280. func (x X) String() string { return Sprintf(&#34;&lt;%s&gt;&#34;, x) }
  281. </pre>
  282. <p>
  283. convert the value before recurring:
  284. </p>
  285. <pre>func (x X) String() string { return Sprintf(&#34;&lt;%s&gt;&#34;, string(x)) }
  286. </pre>
  287. <p>
  288. Infinite recursion can also be triggered by self-referential data
  289. structures, such as a slice that contains itself as an element, if
  290. that type has a String method. Such pathologies are rare, however,
  291. and the package does not protect against them.
  292. </p>
  293. <p>
  294. Explicit argument indexes:
  295. </p>
  296. <p>
  297. In Printf, Sprintf, and Fprintf, the default behavior is for each
  298. formatting verb to format successive arguments passed in the call.
  299. However, the notation [n] immediately before the verb indicates that the
  300. nth one-indexed argument is to be formatted instead. The same notation
  301. before a &#39;*&#39; for a width or precision selects the argument index holding
  302. the value. After processing a bracketed expression [n], subsequent verbs
  303. will use arguments n+1, n+2, etc. unless otherwise directed.
  304. </p>
  305. <p>
  306. For example,
  307. </p>
  308. <pre>fmt.Sprintf(&#34;%[2]d %[1]d\n&#34;, 11, 22)
  309. </pre>
  310. <p>
  311. will yield &#34;22 11&#34;, while
  312. </p>
  313. <pre>fmt.Sprintf(&#34;%[3]*.[2]*[1]f&#34;, 12.0, 2, 6),
  314. </pre>
  315. <p>
  316. equivalent to
  317. </p>
  318. <pre>fmt.Sprintf(&#34;%6.2f&#34;, 12.0),
  319. </pre>
  320. <p>
  321. will yield &#34; 12.00&#34;. Because an explicit index affects subsequent verbs,
  322. this notation can be used to print the same values multiple times
  323. by resetting the index for the first argument to be repeated:
  324. </p>
  325. <pre>fmt.Sprintf(&#34;%d %d %#[1]x %#x&#34;, 16, 17)
  326. </pre>
  327. <p>
  328. will yield &#34;16 17 0x10 0x11&#34;.
  329. </p>
  330. <p>
  331. Format errors:
  332. </p>
  333. <p>
  334. If an invalid argument is given for a verb, such as providing
  335. a string to %d, the generated string will contain a
  336. description of the problem, as in these examples:
  337. </p>
  338. <pre>Wrong type or unknown verb: %!verb(type=value)
  339. Printf(&#34;%d&#34;, hi): %!d(string=hi)
  340. Too many arguments: %!(EXTRA type=value)
  341. Printf(&#34;hi&#34;, &#34;guys&#34;): hi%!(EXTRA string=guys)
  342. Too few arguments: %!verb(MISSING)
  343. Printf(&#34;hi%d&#34;): hi %!d(MISSING)
  344. Non-int for width or precision: %!(BADWIDTH) or %!(BADPREC)
  345. Printf(&#34;%*s&#34;, 4.5, &#34;hi&#34;): %!(BADWIDTH)hi
  346. Printf(&#34;%.*s&#34;, 4.5, &#34;hi&#34;): %!(BADPREC)hi
  347. Invalid or invalid use of argument index: %!(BADINDEX)
  348. Printf(&#34;%*[2]d&#34;, 7): %!d(BADINDEX)
  349. Printf(&#34;%.[2]d&#34;, 7): %!d(BADINDEX)
  350. </pre>
  351. <p>
  352. All errors begin with the string &#34;%!&#34; followed sometimes
  353. by a single character (the verb) and end with a parenthesized
  354. description.
  355. </p>
  356. <p>
  357. If an Error or String method triggers a panic when called by a
  358. print routine, the fmt package reformats the error message
  359. from the panic, decorating it with an indication that it came
  360. through the fmt package. For example, if a String method
  361. calls panic(&#34;bad&#34;), the resulting formatted message will look
  362. like
  363. </p>
  364. <pre>%!s(PANIC=bad)
  365. </pre>
  366. <p>
  367. The %!s just shows the print verb in use when the failure
  368. occurred. If the panic is caused by a nil receiver to an Error
  369. or String method, however, the output is the undecorated
  370. string, &#34;&lt;nil&gt;&#34;.
  371. </p>
  372. <h3 id="hdr-Scanning">Scanning</h3>
  373. <p>
  374. An analogous set of functions scans formatted text to yield
  375. values. Scan, Scanf and Scanln read from os.Stdin; Fscan,
  376. Fscanf and Fscanln read from a specified io.Reader; Sscan,
  377. Sscanf and Sscanln read from an argument string.
  378. </p>
  379. <p>
  380. Scan, Fscan, Sscan treat newlines in the input as spaces.
  381. </p>
  382. <p>
  383. Scanln, Fscanln and Sscanln stop scanning at a newline and
  384. require that the items be followed by a newline or EOF.
  385. </p>
  386. <p>
  387. Scanf, Fscanf and Sscanf require that (after skipping spaces)
  388. newlines in the format are matched by newlines in the input
  389. and vice versa. This behavior differs from the corresponding
  390. routines in C, which uniformly treat newlines as spaces.
  391. </p>
  392. <p>
  393. When scanning with Scanf, Fscanf, and Sscanf, all non-empty
  394. runs of space characters (except newline) are equivalent
  395. to a single space in both the format and the input. With
  396. that proviso, text in the format string must match the input
  397. text; scanning stops if it does not, with the return value
  398. of the function indicating the number of arguments scanned.
  399. </p>
  400. <p>
  401. Scanf, Fscanf, and Sscanf parse the arguments according to a
  402. format string, analogous to that of Printf. For example, %x
  403. will scan an integer as a hexadecimal number, and %v will scan
  404. the default representation format for the value.
  405. </p>
  406. <p>
  407. The formats behave analogously to those of Printf with the
  408. following exceptions:
  409. </p>
  410. <pre>%p is not implemented
  411. %T is not implemented
  412. %e %E %f %F %g %G are all equivalent and scan any floating point or complex value
  413. %s and %v on strings scan a space-delimited token
  414. Flags # and + are not implemented.
  415. </pre>
  416. <p>
  417. The familiar base-setting prefixes 0 (octal) and 0x
  418. (hexadecimal) are accepted when scanning integers without
  419. a format or with the %v verb.
  420. </p>
  421. <p>
  422. Width is interpreted in the input text but there is no
  423. syntax for scanning with a precision (no %5.2f, just %5f).
  424. If width is provided, it applies after leading spaces are
  425. trimmed and specifies the maximum number of runes to read
  426. to satisfy the verb. For example,
  427. </p>
  428. <pre>Sscanf(&#34; 1234567 &#34;, &#34;%5s%d&#34;, &amp;s, &amp;i)
  429. </pre>
  430. <p>
  431. will set s to &#34;12345&#34; and i to 67 while
  432. </p>
  433. <pre>Sscanf(&#34; 12 34 567 &#34;, &#34;%5s%d&#34;, &amp;s, &amp;i)
  434. </pre>
  435. <p>
  436. will set s to &#34;12&#34; and i to 34.
  437. </p>
  438. <p>
  439. In all the scanning functions, a carriage return followed
  440. immediately by a newline is treated as a plain newline
  441. (\r\n means the same as \n).
  442. </p>
  443. <p>
  444. In all the scanning functions, if an operand implements method
  445. Scan (that is, it implements the Scanner interface) that
  446. method will be used to scan the text for that operand. Also,
  447. if the number of arguments scanned is less than the number of
  448. arguments provided, an error is returned.
  449. </p>
  450. <p>
  451. All arguments to be scanned must be either pointers to basic
  452. types or implementations of the Scanner interface.
  453. </p>
  454. <p>
  455. Note: Fscan etc. can read one character (rune) past the input
  456. they return, which means that a loop calling a scan routine
  457. may skip some of the input. This is usually a problem only
  458. when there is no space between input values. If the reader
  459. provided to Fscan implements ReadRune, that method will be used
  460. to read characters. If the reader also implements UnreadRune,
  461. that method will be used to save the character and successive
  462. calls will not lose data. To attach ReadRune and UnreadRune
  463. methods to a reader without that capability, use
  464. bufio.NewReader.
  465. </p>
  466. </div>
  467. </div>
  468. <div id="pkg-index" class="toggleVisible">
  469. <div class="collapsed">
  470. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  471. </div>
  472. <div class="expanded">
  473. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  474. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  475. <div id="manual-nav">
  476. <dl>
  477. <dd><a href="index.html#Errorf">func Errorf(format string, a ...interface{}) error</a></dd>
  478. <dd><a href="index.html#Fprint">func Fprint(w io.Writer, a ...interface{}) (n int, err error)</a></dd>
  479. <dd><a href="index.html#Fprintf">func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)</a></dd>
  480. <dd><a href="index.html#Fprintln">func Fprintln(w io.Writer, a ...interface{}) (n int, err error)</a></dd>
  481. <dd><a href="index.html#Fscan">func Fscan(r io.Reader, a ...interface{}) (n int, err error)</a></dd>
  482. <dd><a href="index.html#Fscanf">func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)</a></dd>
  483. <dd><a href="index.html#Fscanln">func Fscanln(r io.Reader, a ...interface{}) (n int, err error)</a></dd>
  484. <dd><a href="index.html#Print">func Print(a ...interface{}) (n int, err error)</a></dd>
  485. <dd><a href="index.html#Printf">func Printf(format string, a ...interface{}) (n int, err error)</a></dd>
  486. <dd><a href="index.html#Println">func Println(a ...interface{}) (n int, err error)</a></dd>
  487. <dd><a href="index.html#Scan">func Scan(a ...interface{}) (n int, err error)</a></dd>
  488. <dd><a href="index.html#Scanf">func Scanf(format string, a ...interface{}) (n int, err error)</a></dd>
  489. <dd><a href="index.html#Scanln">func Scanln(a ...interface{}) (n int, err error)</a></dd>
  490. <dd><a href="index.html#Sprint">func Sprint(a ...interface{}) string</a></dd>
  491. <dd><a href="index.html#Sprintf">func Sprintf(format string, a ...interface{}) string</a></dd>
  492. <dd><a href="index.html#Sprintln">func Sprintln(a ...interface{}) string</a></dd>
  493. <dd><a href="index.html#Sscan">func Sscan(str string, a ...interface{}) (n int, err error)</a></dd>
  494. <dd><a href="index.html#Sscanf">func Sscanf(str string, format string, a ...interface{}) (n int, err error)</a></dd>
  495. <dd><a href="index.html#Sscanln">func Sscanln(str string, a ...interface{}) (n int, err error)</a></dd>
  496. <dd><a href="index.html#Formatter">type Formatter</a></dd>
  497. <dd><a href="index.html#GoStringer">type GoStringer</a></dd>
  498. <dd><a href="index.html#ScanState">type ScanState</a></dd>
  499. <dd><a href="index.html#Scanner">type Scanner</a></dd>
  500. <dd><a href="index.html#State">type State</a></dd>
  501. <dd><a href="index.html#Stringer">type Stringer</a></dd>
  502. </dl>
  503. </div><!-- #manual-nav -->
  504. <h4>Package files</h4>
  505. <p>
  506. <span style="font-size:90%">
  507. <a href="http://localhost:6060/src/fmt/doc.go">doc.go</a>
  508. <a href="http://localhost:6060/src/fmt/format.go">format.go</a>
  509. <a href="http://localhost:6060/src/fmt/print.go">print.go</a>
  510. <a href="http://localhost:6060/src/fmt/scan.go">scan.go</a>
  511. </span>
  512. </p>
  513. </div><!-- .expanded -->
  514. </div><!-- #pkg-index -->
  515. <div id="pkg-callgraph" class="toggle" style="display: none">
  516. <div class="collapsed">
  517. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  518. </div> <!-- .expanded -->
  519. <div class="expanded">
  520. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  521. <p>
  522. In the call graph viewer below, each node
  523. is a function belonging to this package
  524. and its children are the functions it
  525. calls&mdash;perhaps dynamically.
  526. </p>
  527. <p>
  528. The root nodes are the entry points of the
  529. package: functions that may be called from
  530. outside the package.
  531. There may be non-exported or anonymous
  532. functions among them if they are called
  533. dynamically from another package.
  534. </p>
  535. <p>
  536. Click a node to visit that function's source code.
  537. From there you can visit its callers by
  538. clicking its declaring <code>func</code>
  539. token.
  540. </p>
  541. <p>
  542. Functions may be omitted if they were
  543. determined to be unreachable in the
  544. particular programs or tests that were
  545. analyzed.
  546. </p>
  547. <!-- Zero means show all package entry points. -->
  548. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  549. </div>
  550. </div> <!-- #pkg-callgraph -->
  551. <h2 id="Errorf">func <a href="http://localhost:6060/src/fmt/print.go?s=5673:5723#L201">Errorf</a></h2>
  552. <pre>func Errorf(format <a href="../builtin/index.html#string">string</a>, a ...interface{}) <a href="../builtin/index.html#error">error</a></pre>
  553. <p>
  554. Errorf formats according to a format specifier and returns the string
  555. as a value that satisfies error.
  556. </p>
  557. <h2 id="Fprint">func <a href="http://localhost:6060/src/fmt/print.go?s=6034:6095#L210">Fprint</a></h2>
  558. <pre>func Fprint(w <a href="../io/index.html">io</a>.<a href="../io/index.html#Writer">Writer</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  559. <p>
  560. Fprint formats using the default formats for its operands and writes to w.
  561. Spaces are added between operands when neither is a string.
  562. It returns the number of bytes written and any write error encountered.
  563. </p>
  564. <h2 id="Fprintf">func <a href="http://localhost:6060/src/fmt/print.go?s=4905:4982#L176">Fprintf</a></h2>
  565. <pre>func Fprintf(w <a href="../io/index.html">io</a>.<a href="../io/index.html#Writer">Writer</a>, format <a href="../builtin/index.html#string">string</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  566. <p>
  567. Fprintf formats according to a format specifier and writes to w.
  568. It returns the number of bytes written and any write error encountered.
  569. </p>
  570. <h2 id="Fprintln">func <a href="http://localhost:6060/src/fmt/print.go?s=7163:7226#L242">Fprintln</a></h2>
  571. <pre>func Fprintln(w <a href="../io/index.html">io</a>.<a href="../io/index.html#Writer">Writer</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  572. <p>
  573. Fprintln formats using the default formats for its operands and writes to w.
  574. Spaces are always added between operands and a newline is appended.
  575. It returns the number of bytes written and any write error encountered.
  576. </p>
  577. <h2 id="Fscan">func <a href="http://localhost:6060/src/fmt/scan.go?s=5217:5277#L119">Fscan</a></h2>
  578. <pre>func Fscan(r <a href="../io/index.html">io</a>.<a href="../io/index.html#Reader">Reader</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  579. <p>
  580. Fscan scans text read from r, storing successive space-separated
  581. values into successive arguments. Newlines count as space. It
  582. returns the number of items successfully scanned. If that is less
  583. than the number of arguments, err will report why.
  584. </p>
  585. <h2 id="Fscanf">func <a href="http://localhost:6060/src/fmt/scan.go?s=5891:5967#L139">Fscanf</a></h2>
  586. <pre>func Fscanf(r <a href="../io/index.html">io</a>.<a href="../io/index.html#Reader">Reader</a>, format <a href="../builtin/index.html#string">string</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  587. <p>
  588. Fscanf scans text read from r, storing successive space-separated
  589. values into successive arguments as determined by the format. It
  590. returns the number of items successfully parsed.
  591. Newlines in the input must match newlines in the format.
  592. </p>
  593. <h2 id="Fscanln">func <a href="http://localhost:6060/src/fmt/scan.go?s=5490:5552#L128">Fscanln</a></h2>
  594. <pre>func Fscanln(r <a href="../io/index.html">io</a>.<a href="../io/index.html#Reader">Reader</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  595. <p>
  596. Fscanln is similar to Fscan, but stops scanning at a newline and
  597. after the final item there must be a newline or EOF.
  598. </p>
  599. <h2 id="Print">func <a href="http://localhost:6060/src/fmt/print.go?s=6420:6467#L221">Print</a></h2>
  600. <pre>func Print(a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  601. <p>
  602. Print formats using the default formats for its operands and writes to standard output.
  603. Spaces are added between operands when neither is a string.
  604. It returns the number of bytes written and any write error encountered.
  605. </p>
  606. <h2 id="Printf">func <a href="http://localhost:6060/src/fmt/print.go?s=5229:5292#L186">Printf</a></h2>
  607. <pre>func Printf(format <a href="../builtin/index.html#string">string</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  608. <p>
  609. Printf formats according to a format specifier and writes to standard output.
  610. It returns the number of bytes written and any write error encountered.
  611. </p>
  612. <h2 id="Println">func <a href="http://localhost:6060/src/fmt/print.go?s=7559:7608#L253">Println</a></h2>
  613. <pre>func Println(a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  614. <p>
  615. Println formats using the default formats for its operands and writes to standard output.
  616. Spaces are always added between operands and a newline is appended.
  617. It returns the number of bytes written and any write error encountered.
  618. </p>
  619. <h2 id="Scan">func <a href="http://localhost:6060/src/fmt/scan.go?s=2951:2997#L61">Scan</a></h2>
  620. <pre>func Scan(a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  621. <p>
  622. Scan scans text read from standard input, storing successive
  623. space-separated values into successive arguments. Newlines count
  624. as space. It returns the number of items successfully scanned.
  625. If that is less than the number of arguments, err will report why.
  626. </p>
  627. <h2 id="Scanf">func <a href="http://localhost:6060/src/fmt/scan.go?s=3700:3762#L78">Scanf</a></h2>
  628. <pre>func Scanf(format <a href="../builtin/index.html#string">string</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  629. <p>
  630. Scanf scans text read from standard input, storing successive
  631. space-separated values into successive arguments as determined by
  632. the format. It returns the number of items successfully scanned.
  633. If that is less than the number of arguments, err will report why.
  634. Newlines in the input must match newlines in the format.
  635. The one exception: the verb %c always scans the next rune in the
  636. input, even if it is a space (or tab etc.) or newline.
  637. </p>
  638. <h2 id="Scanln">func <a href="http://localhost:6060/src/fmt/scan.go?s=3155:3203#L67">Scanln</a></h2>
  639. <pre>func Scanln(a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  640. <p>
  641. Scanln is similar to Scan, but stops scanning at a newline and
  642. after the final item there must be a newline or EOF.
  643. </p>
  644. <h2 id="Sprint">func <a href="http://localhost:6060/src/fmt/print.go?s=6663:6699#L227">Sprint</a></h2>
  645. <pre>func Sprint(a ...interface{}) <a href="../builtin/index.html#string">string</a></pre>
  646. <p>
  647. Sprint formats using the default formats for its operands and returns the resulting string.
  648. Spaces are added between operands when neither is a string.
  649. </p>
  650. <h2 id="Sprintf">func <a href="http://localhost:6060/src/fmt/print.go?s=5424:5476#L191">Sprintf</a></h2>
  651. <pre>func Sprintf(format <a href="../builtin/index.html#string">string</a>, a ...interface{}) <a href="../builtin/index.html#string">string</a></pre>
  652. <p>
  653. Sprintf formats according to a format specifier and returns the resulting string.
  654. </p>
  655. <h2 id="Sprintln">func <a href="http://localhost:6060/src/fmt/print.go?s=7816:7854#L259">Sprintln</a></h2>
  656. <pre>func Sprintln(a ...interface{}) <a href="../builtin/index.html#string">string</a></pre>
  657. <p>
  658. Sprintln formats using the default formats for its operands and returns the resulting string.
  659. Spaces are always added between operands and a newline is appended.
  660. </p>
  661. <h2 id="Sscan">func <a href="http://localhost:6060/src/fmt/scan.go?s=4228:4287#L97">Sscan</a></h2>
  662. <pre>func Sscan(str <a href="../builtin/index.html#string">string</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  663. <p>
  664. Sscan scans the argument string, storing successive space-separated
  665. values into successive arguments. Newlines count as space. It
  666. returns the number of items successfully scanned. If that is less
  667. than the number of arguments, err will report why.
  668. </p>
  669. <h2 id="Sscanf">func <a href="http://localhost:6060/src/fmt/scan.go?s=4825:4900#L111">Sscanf</a></h2>
  670. <pre>func Sscanf(str <a href="../builtin/index.html#string">string</a>, format <a href="../builtin/index.html#string">string</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  671. <p>
  672. Sscanf scans the argument string, storing successive space-separated
  673. values into successive arguments as determined by the format. It
  674. returns the number of items successfully parsed.
  675. Newlines in the input must match newlines in the format.
  676. </p>
  677. <h2 id="Sscanln">func <a href="http://localhost:6060/src/fmt/scan.go?s=4460:4521#L103">Sscanln</a></h2>
  678. <pre>func Sscanln(str <a href="../builtin/index.html#string">string</a>, a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  679. <p>
  680. Sscanln is similar to Sscan, but stops scanning at a newline and
  681. after the final item there must be a newline or EOF.
  682. </p>
  683. <h2 id="Formatter">type <a href="http://localhost:6060/src/fmt/print.go?s=1734:1787#L44">Formatter</a></h2>
  684. <pre>type Formatter interface {
  685. Format(f <a href="index.html#State">State</a>, c <a href="../builtin/index.html#rune">rune</a>)
  686. }</pre>
  687. <p>
  688. Formatter is the interface implemented by values with a custom formatter.
  689. The implementation of Format may call Sprint(f) or Fprint(f) etc.
  690. to generate its output.
  691. </p>
  692. <h2 id="GoStringer">type <a href="http://localhost:6060/src/fmt/print.go?s=2313:2361#L61">GoStringer</a></h2>
  693. <pre>type GoStringer interface {
  694. GoString() <a href="../builtin/index.html#string">string</a>
  695. }</pre>
  696. <p>
  697. GoStringer is implemented by any value that has a GoString method,
  698. which defines the Go syntax for that value.
  699. The GoString method is used to print values passed as an operand
  700. to a %#v format.
  701. </p>
  702. <h2 id="ScanState">type <a href="http://localhost:6060/src/fmt/scan.go?s=735:2316#L19">ScanState</a></h2>
  703. <pre>type ScanState interface {
  704. <span class="comment">// ReadRune reads the next rune (Unicode code point) from the input.</span>
  705. <span class="comment">// If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will</span>
  706. <span class="comment">// return EOF after returning the first &#39;\n&#39; or when reading beyond</span>
  707. <span class="comment">// the specified width.</span>
  708. ReadRune() (r <a href="../builtin/index.html#rune">rune</a>, size <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)
  709. <span class="comment">// UnreadRune causes the next call to ReadRune to return the same rune.</span>
  710. UnreadRune() <a href="../builtin/index.html#error">error</a>
  711. <span class="comment">// SkipSpace skips space in the input. Newlines are treated appropriately</span>
  712. <span class="comment">// for the operation being performed; see the package documentation</span>
  713. <span class="comment">// for more information.</span>
  714. SkipSpace()
  715. <span class="comment">// Token skips space in the input if skipSpace is true, then returns the</span>
  716. <span class="comment">// run of Unicode code points c satisfying f(c). If f is nil,</span>
  717. <span class="comment">// !unicode.IsSpace(c) is used; that is, the token will hold non-space</span>
  718. <span class="comment">// characters. Newlines are treated appropriately for the operation being</span>
  719. <span class="comment">// performed; see the package documentation for more information.</span>
  720. <span class="comment">// The returned slice points to shared data that may be overwritten</span>
  721. <span class="comment">// by the next call to Token, a call to a Scan function using the ScanState</span>
  722. <span class="comment">// as input, or when the calling Scan method returns.</span>
  723. Token(skipSpace <a href="../builtin/index.html#bool">bool</a>, f func(<a href="../builtin/index.html#rune">rune</a>) <a href="../builtin/index.html#bool">bool</a>) (token []<a href="../builtin/index.html#byte">byte</a>, err <a href="../builtin/index.html#error">error</a>)
  724. <span class="comment">// Width returns the value of the width option and whether it has been set.</span>
  725. <span class="comment">// The unit is Unicode code points.</span>
  726. Width() (wid <a href="../builtin/index.html#int">int</a>, ok <a href="../builtin/index.html#bool">bool</a>)
  727. <span class="comment">// Because ReadRune is implemented by the interface, Read should never be</span>
  728. <span class="comment">// called by the scanning routines and a valid implementation of</span>
  729. <span class="comment">// ScanState may choose always to return an error from Read.</span>
  730. Read(buf []<a href="../builtin/index.html#byte">byte</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)
  731. }</pre>
  732. <p>
  733. ScanState represents the scanner state passed to custom scanners.
  734. Scanners may do rune-at-a-time scanning or ask the ScanState
  735. to discover the next space-delimited token.
  736. </p>
  737. <h2 id="Scanner">type <a href="http://localhost:6060/src/fmt/scan.go?s=2613:2679#L53">Scanner</a></h2>
  738. <pre>type Scanner interface {
  739. Scan(state <a href="index.html#ScanState">ScanState</a>, verb <a href="../builtin/index.html#rune">rune</a>) <a href="../builtin/index.html#error">error</a>
  740. }</pre>
  741. <p>
  742. Scanner is implemented by any value that has a Scan method, which scans
  743. the input for the representation of a value and stores the result in the
  744. receiver, which must be a pointer to be useful. The Scan method is called
  745. for any argument to Scan, Scanf, or Scanln that implements it.
  746. </p>
  747. <h2 id="State">type <a href="http://localhost:6060/src/fmt/print.go?s=1117:1559#L29">State</a></h2>
  748. <pre>type State interface {
  749. <span class="comment">// Write is the function to call to emit formatted output to be printed.</span>
  750. Write(b []<a href="../builtin/index.html#byte">byte</a>) (ret <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)
  751. <span class="comment">// Width returns the value of the width option and whether it has been set.</span>
  752. Width() (wid <a href="../builtin/index.html#int">int</a>, ok <a href="../builtin/index.html#bool">bool</a>)
  753. <span class="comment">// Precision returns the value of the precision option and whether it has been set.</span>
  754. Precision() (prec <a href="../builtin/index.html#int">int</a>, ok <a href="../builtin/index.html#bool">bool</a>)
  755. <span class="comment">// Flag reports whether the flag c, a character, has been set.</span>
  756. Flag(c <a href="../builtin/index.html#int">int</a>) <a href="../builtin/index.html#bool">bool</a>
  757. }</pre>
  758. <p>
  759. State represents the printer state passed to custom formatters.
  760. It provides access to the io.Writer interface plus information about
  761. the flags and options for the operand&#39;s format specifier.
  762. </p>
  763. <h2 id="Stringer">type <a href="http://localhost:6060/src/fmt/print.go?s=2062:2106#L53">Stringer</a></h2>
  764. <pre>type Stringer interface {
  765. String() <a href="../builtin/index.html#string">string</a>
  766. }</pre>
  767. <p>
  768. Stringer is implemented by any value that has a String method,
  769. which defines the &ldquo;native&rdquo; format for that value.
  770. The String method is used to print values passed as an operand
  771. to any format that accepts a string or to an unformatted printer
  772. such as Print.
  773. </p>
  774. <div id="footer">
  775. Build version go1.6.<br>
  776. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  777. the content of this page is licensed under the
  778. Creative Commons Attribution 3.0 License,
  779. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  780. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  781. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  782. </div>
  783. </div><!-- .container -->
  784. </div><!-- #page -->
  785. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  786. <script type="text/javascript" src="../../lib/godoc/jquery.js"></script>
  787. <script type="text/javascript" src="../../lib/godoc/jquery.treeview.js"></script>
  788. <script type="text/javascript" src="../../lib/godoc/jquery.treeview.edit.js"></script>
  789. <script type="text/javascript" src="../../lib/godoc/godocs.js"></script>
  790. </body>
  791. </html>