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

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#375EAB">
<title>fmt - The Go Programming Language</title>
<link type="text/css" rel="stylesheet" href="../../lib/godoc/style.css">
<link rel="stylesheet" href="../../lib/godoc/jquery.treeview.css">
<script type="text/javascript">window.initFuncs = [];</script>
</head>
<body>
<div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
...
</div><!-- #lowframe -->
<div id="topbar" class="wide"><div class="container">
<div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
<div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
<a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
<form method="GET" action="http://localhost:6060/search">
<div id="menu">
<a href="http://localhost:6060/doc/">Documents</a>
<a href="http://localhost:6060/pkg/">Packages</a>
<a href="http://localhost:6060/project/">The Project</a>
<a href="http://localhost:6060/help/">Help</a>
<a href="http://localhost:6060/blog/">Blog</a>
<input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
</div>
</form>
</div></div>
<div id="page" class="wide">
<div class="container">
<h1>Package fmt</h1>
<div id="nav"></div>
<!--
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<!--
Note: Static (i.e., not template-generated) href and id
attributes start with "pkg-" to make it impossible for
them to conflict with generated attributes (some of which
correspond to Go identifiers).
-->
<script type='text/javascript'>
document.ANALYSIS_DATA = null;
document.CALLGRAPH = null;
</script>
<div id="short-nav">
<dl>
<dd><code>import "fmt"</code></dd>
</dl>
<dl>
<dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
<dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
</dl>
</div>
<!-- The package's Name is printed as title by the top-level template -->
<div id="pkg-overview" class="toggleVisible">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
</div>
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
<p>
Package fmt implements formatted I/O with functions analogous
to C&#39;s printf and scanf. The format &#39;verbs&#39; are derived from C&#39;s but
are simpler.
</p>
<h3 id="hdr-Printing">Printing</h3>
<p>
The verbs:
</p>
<p>
General:
</p>
<pre>%v the value in a default format
when printing structs, the plus flag (%+v) adds field names
%#v a Go-syntax representation of the value
%T a Go-syntax representation of the type of the value
%% a literal percent sign; consumes no value
</pre>
<p>
Boolean:
</p>
<pre>%t the word true or false
</pre>
<p>
Integer:
</p>
<pre>%b base 2
%c the character represented by the corresponding Unicode code point
%d base 10
%o base 8
%q a single-quoted character literal safely escaped with Go syntax.
%x base 16, with lower-case letters for a-f
%X base 16, with upper-case letters for A-F
%U Unicode format: U+1234; same as &#34;U+%04X&#34;
</pre>
<p>
Floating-point and complex constituents:
</p>
<pre>%b decimalless scientific notation with exponent a power of two,
in the manner of strconv.FormatFloat with the &#39;b&#39; format,
e.g. -123456p-78
%e scientific notation, e.g. -1.234456e+78
%E scientific notation, e.g. -1.234456E+78
%f decimal point but no exponent, e.g. 123.456
%F synonym for %f
%g %e for large exponents, %f otherwise
%G %E for large exponents, %F otherwise
</pre>
<p>
String and slice of bytes (treated equivalently with these verbs):
</p>
<pre>%s the uninterpreted bytes of the string or slice
%q a double-quoted string safely escaped with Go syntax
%x base 16, lower-case, two characters per byte
%X base 16, upper-case, two characters per byte
</pre>
<p>
Pointer:
</p>
<pre>%p base 16 notation, with leading 0x
</pre>
<p>
There is no &#39;u&#39; flag. Integers are printed unsigned if they have unsigned type.
Similarly, there is no need to specify the size of the operand (int8, int64).
</p>
<p>
The default format for %v is:
</p>
<pre>bool: %t
int, int8 etc.: %d
uint, uint8 etc.: %d, %x if printed with %#v
float32, complex64, etc: %g
string: %s
chan: %p
pointer: %p
</pre>
<p>
For compound objects, the elements are printed using these rules, recursively,
laid out like this:
</p>
<pre>struct: {field0 field1 ...}
array, slice: [elem0 elem1 ...]
maps: map[key1:value1 key2:value2]
pointer to above: &amp;{}, &amp;[], &amp;map[]
</pre>
<p>
Width is specified by an optional decimal number immediately preceding the verb.
If absent, the width is whatever is necessary to represent the value.
Precision is specified after the (optional) width by a period followed by a
decimal number. If no period is present, a default precision is used.
A period with no following number specifies a precision of zero.
Examples:
</p>
<pre>%f default width, default precision
%9f width 9, default precision
%.2f default width, precision 2
%9.2f width 9, precision 2
%9.f width 9, precision 0
</pre>
<p>
Width and precision are measured in units of Unicode code points,
that is, runes. (This differs from C&#39;s printf where the
units are always measured in bytes.) Either or both of the flags
may be replaced with the character &#39;*&#39;, causing their values to be
obtained from the next operand, which must be of type int.
</p>
<p>
For most values, width is the minimum number of runes to output,
padding the formatted form with spaces if necessary.
</p>
<p>
For strings, byte slices and byte arrays, however, precision
limits the length of the input to be formatted (not the size of
the output), truncating if necessary. Normally it is measured in
runes, but for these types when formatted with the %x or %X format
it is measured in bytes.
</p>
<p>
For floating-point values, width sets the minimum width of the field and
precision sets the number of places after the decimal, if appropriate,
except that for %g/%G it sets the total number of digits. For example,
given 123.45 the format %6.2f prints 123.45 while %.4g prints 123.5.
The default precision for %e and %f is 6; for %g it is the smallest
number of digits necessary to identify the value uniquely.
</p>
<p>
For complex numbers, the width and precision apply to the two
components independently and the result is parenthesized, so %f applied
to 1.2+3.4i produces (1.200000+3.400000i).
</p>
<p>
Other flags:
</p>
<pre>+ always print a sign for numeric values;
guarantee ASCII-only output for %q (%+q)
- pad with spaces on the right rather than the left (left-justify the field)
# alternate format: add leading 0 for octal (%#o), 0x for hex (%#x);
0X for hex (%#X); suppress 0x for %p (%#p);
for %q, print a raw (backquoted) string if strconv.CanBackquote
returns true;
write e.g. U+0078 &#39;x&#39; if the character is printable for %U (%#U).
&#39; &#39; (space) leave a space for elided sign in numbers (% d);
put spaces between bytes printing strings or slices in hex (% x, % X)
0 pad with leading zeros rather than spaces;
for numbers, this moves the padding after the sign
</pre>
<p>
Flags are ignored by verbs that do not expect them.
For example there is no alternate decimal format, so %#d and %d
behave identically.
</p>
<p>
For each Printf-like function, there is also a Print function
that takes no format and is equivalent to saying %v for every
operand. Another variant Println inserts blanks between
operands and appends a newline.
</p>
<p>
Regardless of the verb, if an operand is an interface value,
the internal concrete value is used, not the interface itself.
Thus:
</p>
<pre>var i interface{} = 23
fmt.Printf(&#34;%v\n&#34;, i)
</pre>
<p>
will print 23.
</p>
<p>
Except when printed using the verbs %T and %p, special
formatting considerations apply for operands that implement
certain interfaces. In order of application:
</p>
<p>
1. If the operand is a reflect.Value, the operand is replaced by the
concrete value that it holds, and printing continues with the next rule.
</p>
<p>
2. If an operand implements the Formatter interface, it will
be invoked. Formatter provides fine control of formatting.
</p>
<p>
3. If the %v verb is used with the # flag (%#v) and the operand
implements the GoStringer interface, that will be invoked.
</p>
<p>
If the format (which is implicitly %v for Println etc.) is valid
for a string (%s %q %v %x %X), the following two rules apply:
</p>
<p>
4. If an operand implements the error interface, the Error method
will be invoked to convert the object to a string, which will then
be formatted as required by the verb (if any).
</p>
<p>
5. If an operand implements method String() string, that method
will be invoked to convert the object to a string, which will then
be formatted as required by the verb (if any).
</p>
<p>
For compound operands such as slices and structs, the format
applies to the elements of each operand, recursively, not to the
operand as a whole. Thus %q will quote each element of a slice
of strings, and %6.2f will control formatting for each element
of a floating-point array.
</p>
<p>
However, when printing a byte slice with a string-like verb
(%s %q %x %X), it is treated identically to a string, as a single item.
</p>
<p>
To avoid recursion in cases such as
</p>
<pre>type X string
func (x X) String() string { return Sprintf(&#34;&lt;%s&gt;&#34;, x) }
</pre>
<p>
convert the value before recurring:
</p>
<pre>func (x X) String() string { return Sprintf(&#34;&lt;%s&gt;&#34;, string(x)) }
</pre>
<p>
Infinite recursion can also be triggered by self-referential data
structures, such as a slice that contains itself as an element, if
that type has a String method. Such pathologies are rare, however,
and the package does not protect against them.
</p>
<p>
Explicit argument indexes:
</p>
<p>
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a &#39;*&#39; for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], subsequent verbs
will use arguments n+1, n+2, etc. unless otherwise directed.
</p>
<p>
For example,
</p>
<pre>fmt.Sprintf(&#34;%[2]d %[1]d\n&#34;, 11, 22)
</pre>
<p>
will yield &#34;22 11&#34;, while
</p>
<pre>fmt.Sprintf(&#34;%[3]*.[2]*[1]f&#34;, 12.0, 2, 6),
</pre>
<p>
equivalent to
</p>
<pre>fmt.Sprintf(&#34;%6.2f&#34;, 12.0),
</pre>
<p>
will yield &#34; 12.00&#34;. Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
</p>
<pre>fmt.Sprintf(&#34;%d %d %#[1]x %#x&#34;, 16, 17)
</pre>
<p>
will yield &#34;16 17 0x10 0x11&#34;.
</p>
<p>
Format errors:
</p>
<p>
If an invalid argument is given for a verb, such as providing
a string to %d, the generated string will contain a
description of the problem, as in these examples:
</p>
<pre>Wrong type or unknown verb: %!verb(type=value)
Printf(&#34;%d&#34;, hi): %!d(string=hi)
Too many arguments: %!(EXTRA type=value)
Printf(&#34;hi&#34;, &#34;guys&#34;): hi%!(EXTRA string=guys)
Too few arguments: %!verb(MISSING)
Printf(&#34;hi%d&#34;): hi %!d(MISSING)
Non-int for width or precision: %!(BADWIDTH) or %!(BADPREC)
Printf(&#34;%*s&#34;, 4.5, &#34;hi&#34;): %!(BADWIDTH)hi
Printf(&#34;%.*s&#34;, 4.5, &#34;hi&#34;): %!(BADPREC)hi
Invalid or invalid use of argument index: %!(BADINDEX)
Printf(&#34;%*[2]d&#34;, 7): %!d(BADINDEX)
Printf(&#34;%.[2]d&#34;, 7): %!d(BADINDEX)
</pre>
<p>
All errors begin with the string &#34;%!&#34; followed sometimes
by a single character (the verb) and end with a parenthesized
description.
</p>
<p>
If an Error or String method triggers a panic when called by a
print routine, the fmt package reformats the error message
from the panic, decorating it with an indication that it came
through the fmt package. For example, if a String method
calls panic(&#34;bad&#34;), the resulting formatted message will look
like
</p>
<pre>%!s(PANIC=bad)
</pre>
<p>
The %!s just shows the print verb in use when the failure
occurred. If the panic is caused by a nil receiver to an Error
or String method, however, the output is the undecorated
string, &#34;&lt;nil&gt;&#34;.
</p>
<h3 id="hdr-Scanning">Scanning</h3>
<p>
An analogous set of functions scans formatted text to yield
values. Scan, Scanf and Scanln read from os.Stdin; Fscan,
Fscanf and Fscanln read from a specified io.Reader; Sscan,
Sscanf and Sscanln read from an argument string.
</p>
<p>
Scan, Fscan, Sscan treat newlines in the input as spaces.
</p>
<p>
Scanln, Fscanln and Sscanln stop scanning at a newline and
require that the items be followed by a newline or EOF.
</p>
<p>
Scanf, Fscanf and Sscanf require that (after skipping spaces)
newlines in the format are matched by newlines in the input
and vice versa. This behavior differs from the corresponding
routines in C, which uniformly treat newlines as spaces.
</p>
<p>
When scanning with Scanf, Fscanf, and Sscanf, all non-empty
runs of space characters (except newline) are equivalent
to a single space in both the format and the input. With
that proviso, text in the format string must match the input
text; scanning stops if it does not, with the return value
of the function indicating the number of arguments scanned.
</p>
<p>
Scanf, Fscanf, and Sscanf parse the arguments according to a
format string, analogous to that of Printf. For example, %x
will scan an integer as a hexadecimal number, and %v will scan
the default representation format for the value.
</p>
<p>
The formats behave analogously to those of Printf with the
following exceptions:
</p>
<pre>%p is not implemented
%T is not implemented
%e %E %f %F %g %G are all equivalent and scan any floating point or complex value
%s and %v on strings scan a space-delimited token
Flags # and + are not implemented.
</pre>
<p>
The familiar base-setting prefixes 0 (octal) and 0x
(hexadecimal) are accepted when scanning integers without
a format or with the %v verb.
</p>
<p>
Width is interpreted in the input text but there is no
syntax for scanning with a precision (no %5.2f, just %5f).
If width is provided, it applies after leading spaces are
trimmed and specifies the maximum number of runes to read
to satisfy the verb. For example,
</p>
<pre>Sscanf(&#34; 1234567 &#34;, &#34;%5s%d&#34;, &amp;s, &amp;i)
</pre>
<p>
will set s to &#34;12345&#34; and i to 67 while
</p>
<pre>Sscanf(&#34; 12 34 567 &#34;, &#34;%5s%d&#34;, &amp;s, &amp;i)
</pre>
<p>
will set s to &#34;12&#34; and i to 34.
</p>
<p>
In all the scanning functions, a carriage return followed
immediately by a newline is treated as a plain newline
(\r\n means the same as \n).
</p>
<p>
In all the scanning functions, if an operand implements method
Scan (that is, it implements the Scanner interface) that
method will be used to scan the text for that operand. Also,
if the number of arguments scanned is less than the number of
arguments provided, an error is returned.
</p>
<p>
All arguments to be scanned must be either pointers to basic
types or implementations of the Scanner interface.
</p>
<p>
Note: Fscan etc. can read one character (rune) past the input
they return, which means that a loop calling a scan routine
may skip some of the input. This is usually a problem only
when there is no space between input values. If the reader
provided to Fscan implements ReadRune, that method will be used
to read characters. If the reader also implements UnreadRune,
that method will be used to save the character and successive
calls will not lose data. To attach ReadRune and UnreadRune
methods to a reader without that capability, use
bufio.NewReader.
</p>
</div>
</div>
<div id="pkg-index" class="toggleVisible">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
</div>
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
<!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
<div id="manual-nav">
<dl>
<dd><a href="index.html#Errorf">func Errorf(format string, a ...interface{}) error</a></dd>
<dd><a href="index.html#Fprint">func Fprint(w io.Writer, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Fprintf">func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Fprintln">func Fprintln(w io.Writer, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Fscan">func Fscan(r io.Reader, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Fscanf">func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Fscanln">func Fscanln(r io.Reader, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Print">func Print(a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Printf">func Printf(format string, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Println">func Println(a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Scan">func Scan(a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Scanf">func Scanf(format string, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Scanln">func Scanln(a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Sprint">func Sprint(a ...interface{}) string</a></dd>
<dd><a href="index.html#Sprintf">func Sprintf(format string, a ...interface{}) string</a></dd>
<dd><a href="index.html#Sprintln">func Sprintln(a ...interface{}) string</a></dd>
<dd><a href="index.html#Sscan">func Sscan(str string, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Sscanf">func Sscanf(str string, format string, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Sscanln">func Sscanln(str string, a ...interface{}) (n int, err error)</a></dd>
<dd><a href="index.html#Formatter">type Formatter</a></dd>
<dd><a href="index.html#GoStringer">type GoStringer</a></dd>
<dd><a href="index.html#ScanState">type ScanState</a></dd>
<dd><a href="index.html#Scanner">type Scanner</a></dd>
<dd><a href="index.html#State">type State</a></dd>
<dd><a href="index.html#Stringer">type Stringer</a></dd>
</dl>
</div><!-- #manual-nav -->
<h4>Package files</h4>
<p>
<span style="font-size:90%">
<a href="http://localhost:6060/src/fmt/doc.go">doc.go</a>
<a href="http://localhost:6060/src/fmt/format.go">format.go</a>
<a href="http://localhost:6060/src/fmt/print.go">print.go</a>
<a href="http://localhost:6060/src/fmt/scan.go">scan.go</a>
</span>
</p>
</div><!-- .expanded -->
</div><!-- #pkg-index -->
<div id="pkg-callgraph" class="toggle" style="display: none">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
</div> <!-- .expanded -->
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
<p>
In the call graph viewer below, each node
is a function belonging to this package
and its children are the functions it
calls&mdash;perhaps dynamically.
</p>
<p>
The root nodes are the entry points of the
package: functions that may be called from
outside the package.
There may be non-exported or anonymous
functions among them if they are called
dynamically from another package.
</p>
<p>
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring <code>func</code>
token.
</p>
<p>
Functions may be omitted if they were
determined to be unreachable in the
particular programs or tests that were
analyzed.
</p>
<!-- Zero means show all package entry points. -->
<ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
</div>
</div> <!-- #pkg-callgraph -->
<h2 id="Errorf">func <a href="http://localhost:6060/src/fmt/print.go?s=5673:5723#L201">Errorf</a></h2>
<pre>func Errorf(format <a href="../builtin/index.html#string">string</a>, a ...interface{}) <a href="../builtin/index.html#error">error</a></pre>
<p>
Errorf formats according to a format specifier and returns the string
as a value that satisfies error.
</p>
<h2 id="Fprint">func <a href="http://localhost:6060/src/fmt/print.go?s=6034:6095#L210">Fprint</a></h2>
<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>
<p>
Fprint formats using the default formats for its operands and writes to w.
Spaces are added between operands when neither is a string.
It returns the number of bytes written and any write error encountered.
</p>
<h2 id="Fprintf">func <a href="http://localhost:6060/src/fmt/print.go?s=4905:4982#L176">Fprintf</a></h2>
<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>
<p>
Fprintf formats according to a format specifier and writes to w.
It returns the number of bytes written and any write error encountered.
</p>
<h2 id="Fprintln">func <a href="http://localhost:6060/src/fmt/print.go?s=7163:7226#L242">Fprintln</a></h2>
<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>
<p>
Fprintln formats using the default formats for its operands and writes to w.
Spaces are always added between operands and a newline is appended.
It returns the number of bytes written and any write error encountered.
</p>
<h2 id="Fscan">func <a href="http://localhost:6060/src/fmt/scan.go?s=5217:5277#L119">Fscan</a></h2>
<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>
<p>
Fscan scans text read from r, storing successive space-separated
values into successive arguments. Newlines count as space. It
returns the number of items successfully scanned. If that is less
than the number of arguments, err will report why.
</p>
<h2 id="Fscanf">func <a href="http://localhost:6060/src/fmt/scan.go?s=5891:5967#L139">Fscanf</a></h2>
<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>
<p>
Fscanf scans text read from r, storing successive space-separated
values into successive arguments as determined by the format. It
returns the number of items successfully parsed.
Newlines in the input must match newlines in the format.
</p>
<h2 id="Fscanln">func <a href="http://localhost:6060/src/fmt/scan.go?s=5490:5552#L128">Fscanln</a></h2>
<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>
<p>
Fscanln is similar to Fscan, but stops scanning at a newline and
after the final item there must be a newline or EOF.
</p>
<h2 id="Print">func <a href="http://localhost:6060/src/fmt/print.go?s=6420:6467#L221">Print</a></h2>
<pre>func Print(a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
Print formats using the default formats for its operands and writes to standard output.
Spaces are added between operands when neither is a string.
It returns the number of bytes written and any write error encountered.
</p>
<h2 id="Printf">func <a href="http://localhost:6060/src/fmt/print.go?s=5229:5292#L186">Printf</a></h2>
<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>
<p>
Printf formats according to a format specifier and writes to standard output.
It returns the number of bytes written and any write error encountered.
</p>
<h2 id="Println">func <a href="http://localhost:6060/src/fmt/print.go?s=7559:7608#L253">Println</a></h2>
<pre>func Println(a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
Println formats using the default formats for its operands and writes to standard output.
Spaces are always added between operands and a newline is appended.
It returns the number of bytes written and any write error encountered.
</p>
<h2 id="Scan">func <a href="http://localhost:6060/src/fmt/scan.go?s=2951:2997#L61">Scan</a></h2>
<pre>func Scan(a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
Scan scans text read from standard input, storing successive
space-separated values into successive arguments. Newlines count
as space. It returns the number of items successfully scanned.
If that is less than the number of arguments, err will report why.
</p>
<h2 id="Scanf">func <a href="http://localhost:6060/src/fmt/scan.go?s=3700:3762#L78">Scanf</a></h2>
<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>
<p>
Scanf scans text read from standard input, storing successive
space-separated values into successive arguments as determined by
the format. It returns the number of items successfully scanned.
If that is less than the number of arguments, err will report why.
Newlines in the input must match newlines in the format.
The one exception: the verb %c always scans the next rune in the
input, even if it is a space (or tab etc.) or newline.
</p>
<h2 id="Scanln">func <a href="http://localhost:6060/src/fmt/scan.go?s=3155:3203#L67">Scanln</a></h2>
<pre>func Scanln(a ...interface{}) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
Scanln is similar to Scan, but stops scanning at a newline and
after the final item there must be a newline or EOF.
</p>
<h2 id="Sprint">func <a href="http://localhost:6060/src/fmt/print.go?s=6663:6699#L227">Sprint</a></h2>
<pre>func Sprint(a ...interface{}) <a href="../builtin/index.html#string">string</a></pre>
<p>
Sprint formats using the default formats for its operands and returns the resulting string.
Spaces are added between operands when neither is a string.
</p>
<h2 id="Sprintf">func <a href="http://localhost:6060/src/fmt/print.go?s=5424:5476#L191">Sprintf</a></h2>
<pre>func Sprintf(format <a href="../builtin/index.html#string">string</a>, a ...interface{}) <a href="../builtin/index.html#string">string</a></pre>
<p>
Sprintf formats according to a format specifier and returns the resulting string.
</p>
<h2 id="Sprintln">func <a href="http://localhost:6060/src/fmt/print.go?s=7816:7854#L259">Sprintln</a></h2>
<pre>func Sprintln(a ...interface{}) <a href="../builtin/index.html#string">string</a></pre>
<p>
Sprintln formats using the default formats for its operands and returns the resulting string.
Spaces are always added between operands and a newline is appended.
</p>
<h2 id="Sscan">func <a href="http://localhost:6060/src/fmt/scan.go?s=4228:4287#L97">Sscan</a></h2>
<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>
<p>
Sscan scans the argument string, storing successive space-separated
values into successive arguments. Newlines count as space. It
returns the number of items successfully scanned. If that is less
than the number of arguments, err will report why.
</p>
<h2 id="Sscanf">func <a href="http://localhost:6060/src/fmt/scan.go?s=4825:4900#L111">Sscanf</a></h2>
<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>
<p>
Sscanf scans the argument string, storing successive space-separated
values into successive arguments as determined by the format. It
returns the number of items successfully parsed.
Newlines in the input must match newlines in the format.
</p>
<h2 id="Sscanln">func <a href="http://localhost:6060/src/fmt/scan.go?s=4460:4521#L103">Sscanln</a></h2>
<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>
<p>
Sscanln is similar to Sscan, but stops scanning at a newline and
after the final item there must be a newline or EOF.
</p>
<h2 id="Formatter">type <a href="http://localhost:6060/src/fmt/print.go?s=1734:1787#L44">Formatter</a></h2>
<pre>type Formatter interface {
Format(f <a href="index.html#State">State</a>, c <a href="../builtin/index.html#rune">rune</a>)
}</pre>
<p>
Formatter is the interface implemented by values with a custom formatter.
The implementation of Format may call Sprint(f) or Fprint(f) etc.
to generate its output.
</p>
<h2 id="GoStringer">type <a href="http://localhost:6060/src/fmt/print.go?s=2313:2361#L61">GoStringer</a></h2>
<pre>type GoStringer interface {
GoString() <a href="../builtin/index.html#string">string</a>
}</pre>
<p>
GoStringer is implemented by any value that has a GoString method,
which defines the Go syntax for that value.
The GoString method is used to print values passed as an operand
to a %#v format.
</p>
<h2 id="ScanState">type <a href="http://localhost:6060/src/fmt/scan.go?s=735:2316#L19">ScanState</a></h2>
<pre>type ScanState interface {
<span class="comment">// ReadRune reads the next rune (Unicode code point) from the input.</span>
<span class="comment">// If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will</span>
<span class="comment">// return EOF after returning the first &#39;\n&#39; or when reading beyond</span>
<span class="comment">// the specified width.</span>
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>)
<span class="comment">// UnreadRune causes the next call to ReadRune to return the same rune.</span>
UnreadRune() <a href="../builtin/index.html#error">error</a>
<span class="comment">// SkipSpace skips space in the input. Newlines are treated appropriately</span>
<span class="comment">// for the operation being performed; see the package documentation</span>
<span class="comment">// for more information.</span>
SkipSpace()
<span class="comment">// Token skips space in the input if skipSpace is true, then returns the</span>
<span class="comment">// run of Unicode code points c satisfying f(c). If f is nil,</span>
<span class="comment">// !unicode.IsSpace(c) is used; that is, the token will hold non-space</span>
<span class="comment">// characters. Newlines are treated appropriately for the operation being</span>
<span class="comment">// performed; see the package documentation for more information.</span>
<span class="comment">// The returned slice points to shared data that may be overwritten</span>
<span class="comment">// by the next call to Token, a call to a Scan function using the ScanState</span>
<span class="comment">// as input, or when the calling Scan method returns.</span>
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>)
<span class="comment">// Width returns the value of the width option and whether it has been set.</span>
<span class="comment">// The unit is Unicode code points.</span>
Width() (wid <a href="../builtin/index.html#int">int</a>, ok <a href="../builtin/index.html#bool">bool</a>)
<span class="comment">// Because ReadRune is implemented by the interface, Read should never be</span>
<span class="comment">// called by the scanning routines and a valid implementation of</span>
<span class="comment">// ScanState may choose always to return an error from Read.</span>
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>)
}</pre>
<p>
ScanState represents the scanner state passed to custom scanners.
Scanners may do rune-at-a-time scanning or ask the ScanState
to discover the next space-delimited token.
</p>
<h2 id="Scanner">type <a href="http://localhost:6060/src/fmt/scan.go?s=2613:2679#L53">Scanner</a></h2>
<pre>type Scanner interface {
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>
}</pre>
<p>
Scanner is implemented by any value that has a Scan method, which scans
the input for the representation of a value and stores the result in the
receiver, which must be a pointer to be useful. The Scan method is called
for any argument to Scan, Scanf, or Scanln that implements it.
</p>
<h2 id="State">type <a href="http://localhost:6060/src/fmt/print.go?s=1117:1559#L29">State</a></h2>
<pre>type State interface {
<span class="comment">// Write is the function to call to emit formatted output to be printed.</span>
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>)
<span class="comment">// Width returns the value of the width option and whether it has been set.</span>
Width() (wid <a href="../builtin/index.html#int">int</a>, ok <a href="../builtin/index.html#bool">bool</a>)
<span class="comment">// Precision returns the value of the precision option and whether it has been set.</span>
Precision() (prec <a href="../builtin/index.html#int">int</a>, ok <a href="../builtin/index.html#bool">bool</a>)
<span class="comment">// Flag reports whether the flag c, a character, has been set.</span>
Flag(c <a href="../builtin/index.html#int">int</a>) <a href="../builtin/index.html#bool">bool</a>
}</pre>
<p>
State represents the printer state passed to custom formatters.
It provides access to the io.Writer interface plus information about
the flags and options for the operand&#39;s format specifier.
</p>
<h2 id="Stringer">type <a href="http://localhost:6060/src/fmt/print.go?s=2062:2106#L53">Stringer</a></h2>
<pre>type Stringer interface {
String() <a href="../builtin/index.html#string">string</a>
}</pre>
<p>
Stringer is implemented by any value that has a String method,
which defines the &ldquo;native&rdquo; format for that value.
The String method is used to print values passed as an operand
to any format that accepts a string or to an unformatted printer
such as Print.
</p>
<div id="footer">
Build version go1.6.<br>
Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
the content of this page is licensed under the
Creative Commons Attribution 3.0 License,
and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
<a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
<a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
</div>
</div><!-- .container -->
</div><!-- #page -->
<!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
<script type="text/javascript" src="../../lib/godoc/jquery.js"></script>
<script type="text/javascript" src="../../lib/godoc/jquery.treeview.js"></script>
<script type="text/javascript" src="../../lib/godoc/jquery.treeview.edit.js"></script>
<script type="text/javascript" src="../../lib/godoc/godocs.js"></script>
</body>
</html>