Involved Source Files
Package fmt implements formatted I/O with functions analogous
to C's printf and scanf. The format 'verbs' are derived from C's but
are simpler.
Printing
The verbs:
General:
%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
Boolean:
%t the word true or false
Integer:
%b base 2
%c the character represented by the corresponding Unicode code point
%d base 10
%o base 8
%O base 8 with 0o prefix
%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 "U+%04X"
Floating-point and complex constituents:
%b decimalless scientific notation with exponent a power of two,
in the manner of strconv.FormatFloat with the 'b' 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. Precision is discussed below.
%G %E for large exponents, %F otherwise
%x hexadecimal notation (with decimal power of two exponent), e.g. -0x1.23abcp+20
%X upper-case hexadecimal notation, e.g. -0X1.23ABCP+20
String and slice of bytes (treated equivalently with these verbs):
%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
Slice:
%p address of 0th element in base 16 notation, with leading 0x
Pointer:
%p base 16 notation, with leading 0x
The %b, %d, %o, %x and %X verbs also work with pointers,
formatting the value exactly as if it were an integer.
The default format for %v is:
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
For compound objects, the elements are printed using these rules, recursively,
laid out like this:
struct: {field0 field1 ...}
array, slice: [elem0 elem1 ...]
maps: map[key1:value1 key2:value2 ...]
pointer to above: &{}, &[], &map[]
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:
%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
Width and precision are measured in units of Unicode code points,
that is, runes. (This differs from C's printf where the
units are always measured in bytes.) Either or both of the flags
may be replaced with the character '*', causing their values to be
obtained from the next operand (preceding the one to format),
which must be of type int.
For most values, width is the minimum number of runes to output,
padding the formatted form with spaces if necessary.
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.
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 precision sets the maximum number of significant
digits (trailing zeros are removed). For example, given 12.345 the format
%6.3f prints 12.345 while %.3g prints 12.3. The default precision for %e, %f
and %#g is 6; for %g it is the smallest number of digits necessary to identify
the value uniquely.
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).
Other flags:
+ 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 0b for binary (%#b), 0 for octal (%#o),
0x or 0X for hex (%#x or %#X); suppress 0x for %p (%#p);
for %q, print a raw (backquoted) string if strconv.CanBackquote
returns true;
always print a decimal point for %e, %E, %f, %F, %g and %G;
do not remove trailing zeros for %g and %G;
write e.g. U+0078 'x' if the character is printable for %U (%#U).
' ' (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
Flags are ignored by verbs that do not expect them.
For example there is no alternate decimal format, so %#d and %d
behave identically.
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.
Regardless of the verb, if an operand is an interface value,
the internal concrete value is used, not the interface itself.
Thus:
var i interface{} = 23
fmt.Printf("%v\n", i)
will print 23.
Except when printed using the verbs %T and %p, special
formatting considerations apply for operands that implement
certain interfaces. In order of application:
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.
2. If an operand implements the Formatter interface, it will
be invoked. In this case the interpretation of verbs and flags is
controlled by that implementation.
3. If the %v verb is used with the # flag (%#v) and the operand
implements the GoStringer interface, that will be invoked.
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:
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).
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).
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.
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.
To avoid recursion in cases such as
type X string
func (x X) String() string { return Sprintf("<%s>", x) }
convert the value before recurring:
func (x X) String() string { return Sprintf("<%s>", string(x)) }
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.
When printing a struct, fmt cannot and therefore does not invoke
formatting methods such as Error or String on unexported fields.
Explicit argument indexes:
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 '*' 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.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22 11", while
fmt.Sprintf("%[3]*.[2]*[1]f", 12.0, 2, 6)
equivalent to
fmt.Sprintf("%6.2f", 12.0)
will yield " 12.00". 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:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
Format errors:
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:
Wrong type or unknown verb: %!verb(type=value)
Printf("%d", "hi"): %!d(string=hi)
Too many arguments: %!(EXTRA type=value)
Printf("hi", "guys"): hi%!(EXTRA string=guys)
Too few arguments: %!verb(MISSING)
Printf("hi%d"): hi%!d(MISSING)
Non-int for width or precision: %!(BADWIDTH) or %!(BADPREC)
Printf("%*s", 4.5, "hi"): %!(BADWIDTH)hi
Printf("%.*s", 4.5, "hi"): %!(BADPREC)hi
Invalid or invalid use of argument index: %!(BADINDEX)
Printf("%*[2]d", 7): %!d(BADINDEX)
Printf("%.[2]d", 7): %!d(BADINDEX)
All errors begin with the string "%!" followed sometimes
by a single character (the verb) and end with a parenthesized
description.
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("bad"), the resulting formatted message will look
like
%!s(PANIC=bad)
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, "<nil>".
Scanning
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.
Scan, Fscan, Sscan treat newlines in the input as spaces.
Scanln, Fscanln and Sscanln stop scanning at a newline and
require that the items be followed by a newline or EOF.
Scanf, Fscanf, and Sscanf parse the arguments according to a
format string, analogous to that of Printf. In the text that
follows, 'space' means any Unicode whitespace character
except newline.
In the format string, a verb introduced by the % character
consumes and parses input; these verbs are described in more
detail below. A character other than %, space, or newline in
the format consumes exactly that input character, which must
be present. A newline with zero or more spaces before it in
the format string consumes zero or more spaces in the input
followed by a single newline or the end of the input. A space
following a newline in the format string consumes zero or more
spaces in the input. Otherwise, any run of one or more spaces
in the format string consumes as many spaces as possible in
the input. Unless the run of spaces in the format string
appears adjacent to a newline, the run must consume at least
one space from the input or find the end of the input.
The handling of spaces and newlines differs from that of C's
scanf family: in C, newlines are treated as any other space,
and it is never an error when a run of spaces in the format
string finds no spaces to consume in the input.
The verbs behave analogously to those of Printf.
For example, %x will scan an integer as a hexadecimal number,
and %v will scan the default representation format for the value.
The Printf verbs %p and %T and the flags # and + are not implemented.
For floating-point and complex values, all valid formatting verbs
(%b %e %E %f %F %g %G %x %X and %v) are equivalent and accept
both decimal and hexadecimal notation (for example: "2.3e+7", "0x4.5p-8")
and digit-separating underscores (for example: "3.14159_26535_89793").
Input processed by verbs is implicitly space-delimited: the
implementation of every verb except %c starts by discarding
leading spaces from the remaining input, and the %s verb
(and %v reading into a string) stops consuming input at the first
space or newline character.
The familiar base-setting prefixes 0b (binary), 0o and 0 (octal),
and 0x (hexadecimal) are accepted when scanning integers
without a format or with the %v verb, as are digit-separating
underscores.
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,
Sscanf(" 1234567 ", "%5s%d", &s, &i)
will set s to "12345" and i to 67 while
Sscanf(" 12 34 567 ", "%5s%d", &s, &i)
will set s to "12" and i to 34.
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).
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.
All arguments to be scanned must be either pointers to basic
types or implementations of the Scanner interface.
Like Scanf and Fscanf, Sscanf need not consume its entire input.
There is no way to recover how much of the input string Sscanf used.
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.
errors.goformat.goprint.goscan.go
Code Examples
package main
import (
"fmt"
)
func main() {
const name, id = "bueller", 17
err := fmt.Errorf("user %q (id %d) not found", name, id)
fmt.Println(err.Error())
}
package main
import (
"fmt"
"os"
)
func main() {
const name, age = "Kim", 22
n, err := fmt.Fprint(os.Stdout, name, " is ", age, " years old.\n")
// The n and err return values from Fprint are
// those returned by the underlying io.Writer.
if err != nil {
fmt.Fprintf(os.Stderr, "Fprint: %v\n", err)
}
fmt.Print(n, " bytes written.\n")
}
package main
import (
"fmt"
"os"
)
func main() {
const name, age = "Kim", 22
n, err := fmt.Fprintf(os.Stdout, "%s is %d years old.\n", name, age)
// The n and err return values from Fprintf are
// those returned by the underlying io.Writer.
if err != nil {
fmt.Fprintf(os.Stderr, "Fprintf: %v\n", err)
}
fmt.Printf("%d bytes written.\n", n)
}
package main
import (
"fmt"
"os"
)
func main() {
const name, age = "Kim", 22
n, err := fmt.Fprintln(os.Stdout, name, "is", age, "years old.")
// The n and err return values from Fprintln are
// those returned by the underlying io.Writer.
if err != nil {
fmt.Fprintf(os.Stderr, "Fprintln: %v\n", err)
}
fmt.Println(n, "bytes written.")
}
package main
import (
"fmt"
"os"
"strings"
)
func main() {
var (
i int
b bool
s string
)
r := strings.NewReader("5 true gophers")
n, err := fmt.Fscanf(r, "%d %t %s", &i, &b, &s)
if err != nil {
fmt.Fprintf(os.Stderr, "Fscanf: %v\n", err)
}
fmt.Println(i, b, s)
fmt.Println(n)
}
package main
import (
"fmt"
"io"
"strings"
)
func main() {
s := `dmr 1771 1.61803398875
ken 271828 3.14159`
r := strings.NewReader(s)
var a string
var b int
var c float64
for {
n, err := fmt.Fscanln(r, &a, &b, &c)
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
fmt.Printf("%d: %s, %d, %f\n", n, a, b, c)
}
}
package main
import (
"fmt"
)
func main() {
const name, age = "Kim", 22
fmt.Print(name, " is ", age, " years old.\n")
// It is conventional not to worry about any
// error returned by Print.
}
package main
import (
"fmt"
)
func main() {
const name, age = "Kim", 22
fmt.Printf("%s is %d years old.\n", name, age)
// It is conventional not to worry about any
// error returned by Printf.
}
package main
import (
"fmt"
)
func main() {
const name, age = "Kim", 22
fmt.Println(name, "is", age, "years old.")
// It is conventional not to worry about any
// error returned by Println.
}
package main
import (
"fmt"
"io"
"os"
)
func main() {
const name, age = "Kim", 22
s := fmt.Sprint(name, " is ", age, " years old.\n")
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
}
package main
import (
"fmt"
"io"
"os"
)
func main() {
const name, age = "Kim", 22
s := fmt.Sprintf("%s is %d years old.\n", name, age)
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
}
package main
import (
"fmt"
"io"
"os"
)
func main() {
const name, age = "Kim", 22
s := fmt.Sprintln(name, "is", age, "years old.")
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
}
package main
import (
"fmt"
)
func main() {
var name string
var age int
n, err := fmt.Sscanf("Kim is 22 years old", "%s is %d years old", &name, &age)
if err != nil {
panic(err)
}
fmt.Printf("%d: %s, %d\n", n, name, age)
}
package main
import (
"fmt"
"math"
"time"
)
func main() {
// A basic set of examples showing that %v is the default format, in this
// case decimal for integers, which can be explicitly requested with %d;
// the output is just what Println generates.
integer := 23
// Each of these prints "23" (without the quotes).
fmt.Println(integer)
fmt.Printf("%v\n", integer)
fmt.Printf("%d\n", integer)
// The special verb %T shows the type of an item rather than its value.
fmt.Printf("%T %T\n", integer, &integer)
// Result: int *int
// Println(x) is the same as Printf("%v\n", x) so we will use only Printf
// in the following examples. Each one demonstrates how to format values of
// a particular type, such as integers or strings. We start each format
// string with %v to show the default output and follow that with one or
// more custom formats.
// Booleans print as "true" or "false" with %v or %t.
truth := true
fmt.Printf("%v %t\n", truth, truth)
// Result: true true
// Integers print as decimals with %v and %d,
// or in hex with %x, octal with %o, or binary with %b.
answer := 42
fmt.Printf("%v %d %x %o %b\n", answer, answer, answer, answer, answer)
// Result: 42 42 2a 52 101010
// Floats have multiple formats: %v and %g print a compact representation,
// while %f prints a decimal point and %e uses exponential notation. The
// format %6.2f used here shows how to set the width and precision to
// control the appearance of a floating-point value. In this instance, 6 is
// the total width of the printed text for the value (note the extra spaces
// in the output) and 2 is the number of decimal places to show.
pi := math.Pi
fmt.Printf("%v %g %.2f (%6.2f) %e\n", pi, pi, pi, pi, pi)
// Result: 3.141592653589793 3.141592653589793 3.14 ( 3.14) 3.141593e+00
// Complex numbers format as parenthesized pairs of floats, with an 'i'
// after the imaginary part.
point := 110.7 + 22.5i
fmt.Printf("%v %g %.2f %.2e\n", point, point, point, point)
// Result: (110.7+22.5i) (110.7+22.5i) (110.70+22.50i) (1.11e+02+2.25e+01i)
// Runes are integers but when printed with %c show the character with that
// Unicode value. The %q verb shows them as quoted characters, %U as a
// hex Unicode code point, and %#U as both a code point and a quoted
// printable form if the rune is printable.
smile := '😀'
fmt.Printf("%v %d %c %q %U %#U\n", smile, smile, smile, smile, smile, smile)
// Result: 128512 128512 😀 '😀' U+1F600 U+1F600 '😀'
// Strings are formatted with %v and %s as-is, with %q as quoted strings,
// and %#q as backquoted strings.
placeholders := `foo "bar"`
fmt.Printf("%v %s %q %#q\n", placeholders, placeholders, placeholders, placeholders)
// Result: foo "bar" foo "bar" "foo \"bar\"" `foo "bar"`
// Maps formatted with %v show keys and values in their default formats.
// The %#v form (the # is called a "flag" in this context) shows the map in
// the Go source format. Maps are printed in a consistent order, sorted
// by the values of the keys.
isLegume := map[string]bool{
"peanut": true,
"dachshund": false,
}
fmt.Printf("%v %#v\n", isLegume, isLegume)
// Result: map[dachshund:false peanut:true] map[string]bool{"dachshund":false, "peanut":true}
// Structs formatted with %v show field values in their default formats.
// The %+v form shows the fields by name, while %#v formats the struct in
// Go source format.
person := struct {
Name string
Age int
}{"Kim", 22}
fmt.Printf("%v %+v %#v\n", person, person, person)
// Result: {Kim 22} {Name:Kim Age:22} struct { Name string; Age int }{Name:"Kim", Age:22}
// The default format for a pointer shows the underlying value preceded by
// an ampersand. The %p verb prints the pointer value in hex. We use a
// typed nil for the argument to %p here because the value of any non-nil
// pointer would change from run to run; run the commented-out Printf
// call yourself to see.
pointer := &person
fmt.Printf("%v %p\n", pointer, (*int)(nil))
// Result: &{Kim 22} 0x0
// fmt.Printf("%v %p\n", pointer, pointer)
// Result: &{Kim 22} 0x010203 // See comment above.
// Arrays and slices are formatted by applying the format to each element.
greats := [5]string{"Kitano", "Kobayashi", "Kurosawa", "Miyazaki", "Ozu"}
fmt.Printf("%v %q\n", greats, greats)
// Result: [Kitano Kobayashi Kurosawa Miyazaki Ozu] ["Kitano" "Kobayashi" "Kurosawa" "Miyazaki" "Ozu"]
kGreats := greats[:3]
fmt.Printf("%v %q %#v\n", kGreats, kGreats, kGreats)
// Result: [Kitano Kobayashi Kurosawa] ["Kitano" "Kobayashi" "Kurosawa"] []string{"Kitano", "Kobayashi", "Kurosawa"}
// Byte slices are special. Integer verbs like %d print the elements in
// that format. The %s and %q forms treat the slice like a string. The %x
// verb has a special form with the space flag that puts a space between
// the bytes.
cmd := []byte("a⌘")
fmt.Printf("%v %d %s %q %x % x\n", cmd, cmd, cmd, cmd, cmd, cmd)
// Result: [97 226 140 152] [97 226 140 152] a⌘ "a⌘" 61e28c98 61 e2 8c 98
// Types that implement Stringer are printed the same as strings. Because
// Stringers return a string, we can print them using a string-specific
// verb such as %q.
now := time.Unix(123456789, 0).UTC() // time.Time implements fmt.Stringer.
fmt.Printf("%v %q\n", now, now)
// Result: 1973-11-29 21:33:09 +0000 UTC "1973-11-29 21:33:09 +0000 UTC"
}
package main
import (
"fmt"
"math"
)
func main() {
a, b := 3.0, 4.0
h := math.Hypot(a, b)
// Print inserts blanks between arguments when neither is a string.
// It does not add a newline to the output, so we add one explicitly.
fmt.Print("The vector (", a, b, ") has length ", h, ".\n")
// Println always inserts spaces between its arguments,
// so it cannot be used to produce the same output as Print in this case;
// its output has extra spaces.
// Also, Println always adds a newline to the output.
fmt.Println("The vector (", a, b, ") has length", h, ".")
// Printf provides complete control but is more complex to use.
// It does not add a newline to the output, so we add one explicitly
// at the end of the format specifier string.
fmt.Printf("The vector (%g %g) has length %g.\n", a, b, h)
}
Package-Level Type Names (total 16, in which 6 are exported)
/* sort exporteds by: | */
Formatter is implemented by any value that has a Format method.
The implementation controls how State and rune are interpreted,
and may call Sprint(f) or Fprint(f) etc. to generate its output.
( T) Format(f State, verb rune)
*math/big.Float
*math/big.Int
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.
( T) GoString() string
*vendor/golang.org/x/net/dns/dnsmessage.AAAAResource
*vendor/golang.org/x/net/dns/dnsmessage.AResource
vendor/golang.org/x/net/dns/dnsmessage.Class
*vendor/golang.org/x/net/dns/dnsmessage.CNAMEResource
*vendor/golang.org/x/net/dns/dnsmessage.Header
*vendor/golang.org/x/net/dns/dnsmessage.Message
*vendor/golang.org/x/net/dns/dnsmessage.MXResource
*vendor/golang.org/x/net/dns/dnsmessage.Name
*vendor/golang.org/x/net/dns/dnsmessage.NSResource
vendor/golang.org/x/net/dns/dnsmessage.OpCode
*vendor/golang.org/x/net/dns/dnsmessage.Option
*vendor/golang.org/x/net/dns/dnsmessage.OPTResource
*vendor/golang.org/x/net/dns/dnsmessage.PTRResource
*vendor/golang.org/x/net/dns/dnsmessage.Question
vendor/golang.org/x/net/dns/dnsmessage.RCode
*vendor/golang.org/x/net/dns/dnsmessage.Resource
vendor/golang.org/x/net/dns/dnsmessage.ResourceBody(interface)
*vendor/golang.org/x/net/dns/dnsmessage.ResourceHeader
*vendor/golang.org/x/net/dns/dnsmessage.SOAResource
*vendor/golang.org/x/net/dns/dnsmessage.SRVResource
*vendor/golang.org/x/net/dns/dnsmessage.TXTResource
vendor/golang.org/x/net/dns/dnsmessage.Type
encoding/binary.bigEndian
encoding/binary.littleEndian
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.
( T) Scan(state ScanState, verb rune) error
*math/big.Float
*math/big.Int
*math/big.Rat
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.
Because ReadRune is implemented by the interface, Read should never be
called by the scanning routines and a valid implementation of
ScanState may choose always to return an error from Read.
ReadRune reads the next rune (Unicode code point) from the input.
If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will
return EOF after returning the first '\n' or when reading beyond
the specified width.
SkipSpace skips space in the input. Newlines are treated appropriately
for the operation being performed; see the package documentation
for more information.
Token skips space in the input if skipSpace is true, then returns the
run of Unicode code points c satisfying f(c). If f is nil,
!unicode.IsSpace(c) is used; that is, the token will hold non-space
characters. Newlines are treated appropriately for the operation being
performed; see the package documentation for more information.
The returned slice points to shared data that may be overwritten
by the next call to Token, a call to a Scan function using the ScanState
as input, or when the calling Scan method returns.
UnreadRune causes the next call to ReadRune to return the same rune.
Width returns the value of the width option and whether it has been set.
The unit is Unicode code points.
*ss
math/big.byteReader
T : io.Reader
T : io.RuneReader
T : io.RuneScanner
func Scanner.Scan(state ScanState, verb rune) error
func math/big.(*Float).Scan(s ScanState, ch rune) error
func math/big.(*Int).Scan(s ScanState, ch rune) error
func math/big.(*Rat).Scan(s ScanState, ch rune) error
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's format specifier.
Flag reports whether the flag c, a character, has been set.
Precision returns the value of the precision option and whether it has been set.
Width returns the value of the width option and whether it has been set.
Write is the function to call to emit formatted output to be printed.
*pp
T : io.Writer
func Formatter.Format(f State, verb rune)
func math/big.(*Float).Format(s State, format rune)
func math/big.(*Int).Format(s State, ch rune)
func math/big.writeMultiple(s State, text string, count int)
A fmt is the raw formatter used by Printf etc.
It prints into a buffer that must be set up separately.
buf*bufferfmtFlagsfmtFlagsfmtFlags.minusboolfmtFlags.plusbool
For the formats %+v %#v, we set the plusV/sharpV flags
and clear the plus/sharp flags since %+v and %#v are in effect
different, flagless formats set at the top level.
fmtFlags.precPresentboolfmtFlags.sharpboolfmtFlags.sharpVboolfmtFlags.spaceboolfmtFlags.widPresentboolfmtFlags.zerobool
intbuf is large enough to store %b of an int64 with a sign and
avoids padding at the end of the struct on 32 bit architectures.
// precision
// width
(*T) clearflags()
fmtBoolean formats a boolean.
fmtBs formats the byte slice b as if it was formatted as string with fmtS.
fmtBx formats a byte slice as a hexadecimal encoding of its bytes.
fmtC formats an integer as a Unicode character.
If the character is not valid Unicode, it will print '\ufffd'.
fmtFloat formats a float64. It assumes that verb is a valid format specifier
for strconv.AppendFloat and therefore fits into a byte.
fmtInteger formats signed and unsigned integers.
fmtQ formats a string as a double-quoted, escaped Go string constant.
If f.sharp is set a raw (backquoted) string may be returned instead
if the string does not contain any control characters other than tab.
fmtQc formats an integer as a single-quoted, escaped Go character constant.
If the character is not valid Unicode, it will print '\ufffd'.
fmtS formats a string.
fmtSbx formats a string or byte slice as a hexadecimal encoding of its bytes.
fmtSx formats a string as a hexadecimal encoding of its bytes.
fmtUnicode formats a uint64 as "U+0078" or with f.sharp set as "U+0078 'x'".
(*T) init(buf *buffer)
pad appends b to f.buf, padded on left (!f.minus) or right (f.minus).
padString appends s to f.buf, padded on left (!f.minus) or right (f.minus).
truncate truncates the byte slice b as a string of the specified precision, if present.
truncate truncates the string s to the specified precision, if present.
writePadding generates n bytes of padding.
pp is used to store a printer's state and is reused with sync.Pool to avoid allocations.
arg holds the current item, as an interface{}.
bufbuffer
erroring is set when printing an error string to guard against calling handleMethods.
fmt is used to format basic items such as integers or strings.
goodArgNum records whether the most recent reordering directive was valid.
panicking is set by catchPanic to avoid infinite panic, recover, panic, ... recursion.
reordered records whether the format string used argument reordering.
value is used instead of arg for reflect values.
wrapErrs is set when the format string may contain a %w verb.
wrappedErr records the target of the %w verb.
(*T) Flag(b int) bool(*T) Precision() (prec int, ok bool)(*T) Width() (wid int, ok bool)
Implement Write so we can call Fprintf on a pp (through State), for
recursive use in custom verbs.
Implement WriteString so that we can call io.WriteString
on a pp (through state), for efficiency.
argNumber returns the next argument to evaluate, which is either the value of the passed-in
argNum or the value of the bracketed integer that begins format[i:]. It also returns
the new value of i, that is, the index of the next byte of the format to process.
(*T) badArgNum(verb rune)(*T) badVerb(verb rune)(*T) catchPanic(arg interface{}, verb rune, method string)(*T) doPrint(a []interface{})(*T) doPrintf(format string, a []interface{})
doPrintln is like doPrint but always adds a space between arguments
and a newline after the last argument.
fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x or
not, as requested, by temporarily setting the sharp flag.
(*T) fmtBool(v bool, verb rune)(*T) fmtBytes(v []byte, verb rune, typeString string)
fmtComplex formats a complex number v with
r = real(v) and j = imag(v) as (r+ji) using
fmtFloat for r and j formatting.
fmtFloat formats a float. The default precision for each verb
is specified as last argument in the call to fmt_float.
fmtInteger formats a signed or unsigned integer.
(*T) fmtPointer(value reflect.Value, verb rune)(*T) fmtString(v string, verb rune)
free saves used pp structs in ppFree; avoids an allocation per invocation.
(*T) handleMethods(verb rune) (handled bool)(*T) missingArg(verb rune)(*T) printArg(arg interface{}, verb rune)
printValue is similar to printArg but starts with a reflect value, not an interface{} value.
It does not handle 'p' and 'T' verbs because these should have been already handled by printArg.
(*T) unknownType(v reflect.Value)
*T : State
*T : io.StringWriter
*T : io.Writer
func newPrinter() *pp
readRune is a structure to enable reading UTF-8 encoded code points
from an io.Reader. It is used if the Reader given to the scanner does
not already implement io.RuneScanner.
// used only inside ReadRune
// if >=0 next rune; when <0 is ^(previous Rune)
// bytes left over
// number of bytes in pendBuf; only >0 for bad UTF-8
readerio.Reader
ReadRune returns the next UTF-8 encoded code point from the
io.Reader inside r.
(*T) UnreadRune() error
readByte returns the next byte from the input, which may be
left over from a previous read if the UTF-8 was ill-formed.
*T : io.RuneReader
*T : io.RuneScanner
scanError represents an error generated by the scanning software.
It's used as a unique signature to identify such errors when recovering.
errerror
ss is the internal implementation of ScanState.
// already read EOF
// token accumulator
// runes consumed so far.
// where to read input
ssavessave
// max value of ss.count for this arg; argLimit <= limit
// max value of ss.count.
// width of this arg.
// whether newline terminates scan
// whether newline counts as white space
// is or was a part of an actual ss.
The Read method is only in ScanState so that ScanState
satisfies io.Reader. It will never be called when used as
intended, so there is no need to make it actually work.
(*T) ReadRune() (r rune, size int, err error)
SkipSpace provides Scan methods the ability to skip space and newline
characters in keeping with the current scanning mode set by format strings
and Scan/Scanln.
(*T) Token(skipSpace bool, f func(rune) bool) (tok []byte, err error)(*T) UnreadRune() error(*T) Width() (wid int, ok bool)
accept checks the next rune in the input. If it's a byte (sic) in the string, it puts it in the
buffer and returns true. Otherwise it return false.
advance determines whether the next characters in the input match
those of the format. It returns the number of bytes (sic) consumed
in the format. All runs of space characters in either input or
format behave as a single space. Newlines are special, though:
newlines in the format must match those in the input and vice versa.
This routine also handles the %% case. If the return value is zero,
either format starts with a % (with no following %) or the input
is empty. If it is negative, the input did not match the string.
complexTokens returns the real and imaginary parts of the complex number starting here.
The number might be parenthesized and has the format (N+Ni) where N is a floating-point
number and there are no spaces within.
consume reads the next rune in the input and reports whether it is in the ok string.
If accept is true, it puts the character into the input token.
convertFloat converts the string to a float64value.
convertString returns the string represented by the next input characters.
The format of the input is determined by the verb.
doScan does the real work for scanning without a format string.
doScanf does the real work when scanning with a format string.
At the moment, it handles only pointers to basic types.
(*T) error(err error)(*T) errorString(err string)
floatToken returns the floating-point number starting here, no longer than swid
if the width is specified. It's not rigorous about syntax because it doesn't check that
we have at least some digits, but Atof will do that.
free saves used ss structs in ssFree; avoid an allocation per invocation.
getBase returns the numeric base represented by the verb and its digit string.
The public method returns an error; this private one panics.
If getRune reaches EOF, the return value is EOF (-1).
hexByte returns the next hex-encoded (two-character) byte from the input.
It returns ok==false if the next bytes in the input do not encode a hex byte.
If the first byte is hex and the second is not, processing stops.
hexString returns the space-delimited hexpair-encoded string.
mustReadRune turns io.EOF into a panic(io.ErrUnexpectedEOF).
It is called in cases such as string scanning where an EOF is a
syntax error.
(*T) notEOF()
okVerb verifies that the verb is present in the list, setting s.err appropriately if not.
peek reports whether the next character is in the ok string, without consuming it.
quotedString returns the double- or back-quoted string represented by the next input characters.
scanBasePrefix reports whether the integer begins with a base prefix
and returns the base, digit string, and whether a zero was found.
It is called only if the verb is %v.
scanBool returns the value of the boolean represented by the next token.
convertComplex converts the next token to a complex128 value.
The atof argument is a type-specific reader for the underlying type.
If we're reading complex64, atof will parse float32s and convert them
to float64's to avoid reproducing this code for each complex type.
scanInt returns the value of the integer represented by the next
token, checking for overflow. Any error is stored in s.err.
scanNumber returns the numerical string with specified digits starting here.
scanOne scans a single value, deriving the scanner from the type of the argument.
scanPercent scans a literal percent character.
scanRune returns the next rune value in the input.
scanUint returns the value of the unsigned integer represented
by the next token, checking for overflow. Any error is stored in s.err.
token returns the next space-delimited string from the input. It
skips white space. For Scanln, it stops at newlines. For Scan,
newlines are treated as spaces.
*T : ScanState
*T : io.Reader
*T : io.RuneReader
*T : io.RuneScanner
func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave)
ssave holds the parts of ss that need to be
saved and restored on recursive scans.
// max value of ss.count for this arg; argLimit <= limit
// max value of ss.count.
// width of this arg.
// whether newline terminates scan
// whether newline counts as white space
// is or was a part of an actual ss.
func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave)
Package-Level Functions (total 32, in which 19 are exported)
Errorf formats according to a format specifier and returns the string as a
value that satisfies error.
If the format specifier includes a %w verb with an error operand,
the returned error will implement an Unwrap method returning the operand. It is
invalid to include more than one %w verb or to supply it with an operand
that does not implement the error interface. The %w verb is otherwise
a synonym for %v.
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.
Fprintf formats according to a format specifier and writes to w.
It returns the number of bytes written and any write error encountered.
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.
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.
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.
Fscanln is similar to Fscan, but stops scanning at a newline and
after the final item there must be a newline or EOF.
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.
Printf formats according to a format specifier and writes to standard output.
It returns the number of bytes written and any write error encountered.
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.
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.
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.
Scanln is similar to Scan, but stops scanning at a newline and
after the final item there must be a newline or EOF.
Sprint formats using the default formats for its operands and returns the resulting string.
Spaces are added between operands when neither is a string.
Sprintf formats according to a format specifier and returns the resulting string.
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.
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.
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.
Sscanln is similar to Sscan, but stops scanning at a newline and
after the final item there must be a newline or EOF.
errorHandler turns local panics into error returns.
getField gets the i'th field of the struct value.
If the field is itself is an interface, return a value for
the thing inside the interface, not the interface itself.
newPrinter allocates a new pp struct or grabs a cached one.
newScanState allocates a new ss struct or grab a cached one.
notSpace is the default scanning function used in Token.
parseArgNumber returns the value of the bracketed number, minus 1
(explicit argument numbers are one-indexed but we want zero-indexed).
The opening bracket is known to be present at format[0].
The returned values are the index, the number of bytes to consume
up to the closing paren, if present, and whether the number parsed
ok. The bytes to consume will be 1 if no closing paren is present.
parsenum converts ASCII to integer. num is 0 (and isnum is false) if no number present.
tooLarge reports whether the magnitude of the integer is
too large to be used as a formatting width or precision.
Package-Level Variables (total 5, none are exported)
The pages are generated with Goldsv0.4.2. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.