FMTPackage
Import "FMT"
IntroductionBytes
Package FMT contains formatting I/O functions, similar to C-language printf and scanf. The rules of the format string come from C but are simpler.
Output
Format:
General:
The value of % v basic format. When the struct is output, the extension sign (% + V) adds the member name. The value in a default format.
When printing structs, The plus flag (% + V) adds Field Names
The Go syntax of the % # V value.
The Go syntax of the % T value type.
% Percent.
Boolean:
% T value true or false
Integer:
% B binary representation
Unicode characters corresponding to % C values
% D in decimal format
% O octal Representation
% Q single quotes
% X in hexadecimal notation, using a-f
% X hexadecimal representation, using A-F
% U unicode format: U + 1234, equivalent to "U + % 04x"
Floating point:
The scientific notation of % B without decimal part and two-digit index is consistent with the 'B' conversion format of strconv. formatfloat. Example:-123456p-78
% E scientific notation, for example:-1234.456e + 78
% E scientific notation, for example:-1234.456e + 78
% F has a fractional part but no exponent part, for example: 123.456
% G uses % E or % F format based on actual conditions (for more concise output)
% G uses % E or % F format based on actual conditions (for more concise output)
String and byte slice types:
% S directly outputs a string or [] Byte
% Q double quotation mark string
% X each byte is expressed in two-character hexadecimal notation (in lower case a-f)
% X each byte is expressed in two-character hexadecimal notation (in uppercase A-F)
Pointer:
Hexadecimal number representation starting with % P 0x
Wood has a 'u' sign. If it is a non-type Integer, it will naturally print the non-Type format. Similarly, there is no need to distinguish the size of the operand (int8, int64 ).
Width and precision formatting control refers to the number of unicode encoded characters (unlike C's printf, these two factors refer to the number of bytes .) Both of them can be replaced by the '*' sign (either one or both). In this case, their values will be controlled by the following parameter. This operand must be an integer.
For numbers, set the width to the total length, and set the precision to the decimal part length. For example, output 123.45 in the format of % 6.2f.
For a string, the width is the minimum number of output characters. If not, it is filled with spaces. Precision is the maximum number of output characters. If the number is exceeded, it is truncated.
Other symbols:
+ Always outputs positive and negative numbers of values. % Q (% + q) ensures pure ASCII code output.
-Fill the blank space on the right side instead of the default left side.
# Switch format: Add 0 (% # O) before octal, and 0x (% # X) or 0x (% # X) before hexadecimal ); remove the pointer 0x (% # P );
If possible, output a non-modifier string for % Q (% # Q;
For % u (% # U), if the corresponding value is a printable character, this character is output.
''For the number (% d) space, a space is left before the number and the plus and minus signs of the number are ignored;
Slices and strings (% x, % x) are output in hexadecimal notation.
0. Use the leading zero instead of spaces to fill the gap.
Each function similar to printf has the same print function, which does not require a format string and is equivalent to setting each parameter to % v. Another variant, println, will add spaces between parameters and wrap the line after the output ends.
If the parameter is an interface value, the internal implementation value will be used instead of the interface itself, and the % v parameter will not be used. As follows:
VaR I interface {} = 23
FMT. printf ("% V \ n", I)
Output 23.
If the parameter implements the formatter interface, this interface can be used to better control formatting.
If the format (flag is an implicit % V for println, etc.) is dedicated to strings (% S % Q % v % x), the following rules are also provided:
1. If a parameter implements the error interface, the error method is used to convert the target to a string and then formatted as required.
2. If the parameter provides the string method, this method is used to convert the target to a string and format it according to the given format mark.
To avoid possible recursive loops, for example:
Type X string
Func (x) string () string {return sprintf ("<% S>", x )}
The values will be converted before recursive loops:
Func (x) string () string {return sprintf ("<% S>", string (x ))}
Incorrect format:
If an incorrect format flag is provided, for example, the % d flag is provided for a string, the generated string will contain a description of the problem, as shown in the following example:
Error or unknown format flag: %! Verb (type = value)
Printf ("% d", hi): %! D (string = Hi)
Too many parameters: %! (Extra type = value)
Printf ("hi", "guys"): Hi %! (Extra string = guys)
Parameter missing: %! Verb (missing)
Printf ("Hi % d"): Hi %! D (missing)
Provide width and precision with non-integers: %! (Badwidth) or %! (Badprec)
Printf ("% * s", 4.5, "Hi"): %! (Badwidth) Hi
Printf ("%. * s", 4.5, "Hi"): %! (Badprec) Hi
"%!" Is used for all errors! "Start, (followed by the single-character format mark) ending with an error description surrounded by parentheses.
Input
A series of similar functions read formatted text and generate values. Scan, scanf and scanln from OS. stdin reading; fscan, fscanf and fscanln from specific Io. reader reading; sscan, sscanf, and sscanln reading from strings; scanln, fscanln, and sscanln reading ends when changing rows, and data must appear continuously; scanf, fscanf and sscanf read an entire line to match the format string. Other functions will wrap the line and look at the space.
Scanf, fscanf, and sscanf parse data according to the format string, similar to printf. For example, % x reads a hexadecimal number, and % V reads the default value.
The format is similar to printf, with the following exceptions:
% P not provided
% T not provided
% E % F % G is equivalent and can read any floating point number or composite number (non-plural, the exponential number represented by scientific Notation)
When the % s and % vcharacter strings are read in these two formats, They will end with spaces.
If the format is not set or % V is used to read integers, if the prefix is 0 (octal) or 0x (hexadecimal), it will be read in the corresponding hexadecimal format.
Width is interpreted in the input (% 5s means that a maximum of five characters can be read from the input and assigned to a string), but the input series functions do not have syntax for interpreting accuracy (wood has % 5.2f, only % 5f ).
The format string in the input series functions. All non-empty blank characters (except line breaks) are equivalent to 1 blank character in both the input and format strings. The format string must match the input text. If it does not match, the system will stop reading data and return the number of parameters already assigned by the function.
For all scan functions, if the parameter contains the scan method (or implements the dig Interface), this parameter will be used to read text. In addition, if the number of parameters filled in is less than the number of provided parameters, an error is returned.
All parameters to be input should be basic type or data type pointers that implement the callback interface.
Note: fscan and other functions can read and return the required characters from the input, which means that a circular reader may skip part of the input data. When there is no blank data, a problem may occur. If read is provided to the fscan functions readrune method, this method can be used to read characters. If the reader also provides the unreadrune method, this method is used to save characters so that successful calls do not lose data. To add these two methods to a reader without these functions, use bufio. newreader.
Directory
Func errorf (Format String, A... interface {}) Error
Func fprint (W Io. writer, a... interface {}) (N int, err error)
Func fprintf (W Io. Writer, Format String, A... interface {}) (N int, err error)
Func fprintln (W Io. writer, a... interface {}) (N int, err error)
Func fscan (r Io. reader, a... interface {}) (N int, err error)
Func fscanf (r Io. Reader, Format String, A... interface {}) (N int, err error)
Func fscanln (r Io. reader, a... interface {}) (N int, err error)
Func print (a... interface {}) (N int, err error)
Func printf (Format String, A... interface {}) (N int, err error)
Func println (a... interface {}) (N int, err error)
Func scan (a... interface {}) (N int, err error)
Func scanf (Format String, A... interface {}) (N int, err error)
Func scanln (a... interface {}) (N int, err error)
Func Sprint (a... interface {}) String
Func sprintf (Format String, A... interface {}) String
Func sprintln (a... interface {}) String
Func sscan (STR string, A... interface {}) (N int, err error)
Func sscanf (STR string, Format String, A... interface {}) (N int, err error)
Func sscanln (STR string, A... interface {}) (N int, err error)
Type formatter
Type gostringer
Type scanstate
Type Encoding
Type state
Type stringer
Package files
Doc. Go format. Go print. Go scan. Go
Func errorf
Func errorf (Format String, A... interface {}) Error
Errorf generates a string based on the format string and parameter table, which meets the error interface.
Func fprint
Func fprint (W Io. writer, a... interface {}) (N int, err error)
Fprint writes all parameters in the default format to W. If neither of the adjacent parameters is a string, a blank space is added between the parameters. The function returns the number of bytes written and any errors encountered.
Func fprintf
Func fprintf (W Io. Writer, Format String, A... interface {}) (N int, err error)
Fprintf writes the parameter to W based on the format string. The function returns the number of bytes written and any errors encountered.
Func fprintln
Func fprintln (W Io. writer, a... interface {}) (N int, err error)
Fprintln writes all parameters in the default format to W and adds a line break at the end. If neither of the adjacent parameters is a string, a blank space is added between the parameters. The function returns the number of bytes written and any errors encountered.
Func fscan
Func fscan (r Io. reader, a... interface {}) (N int, err error)
Fscan reads text from R and stores consecutive blank split data into continuous parameters. Line feed is regarded as blank. It returns the number of successfully read parameters. If the number is less than the number of provided parameters, the returned value err will report the cause.
Func fscanf
Func fscanf (r Io. Reader, Format String, A... interface {}) (N int, err error)
Fscanf reads text from R and stores data in parameters according to the format string sequence. It returns the number of successfully parsed and saved parameters.
Func fscanln
Func fscanln (r Io. reader, a... interface {}) (N int, err error)
Fscanln is similar to fscanf, but it is aborted with a line break. When the last line is saved, a line break or an ending character must be read.
Func print
Func print (a... interface {}) (N int, err error)
Print writes all parameters to the standard output in the default format. If neither of the adjacent parameters is a string, a blank space is added between the parameters. The function returns the number of bytes written and any errors encountered.
Func printf
Func printf (Format String, A... interface {}) (N int, err error)
Printf writes parameters to the standard output according to the format string. The function returns the number of bytes written and any errors encountered.
Func println
Func println (a... interface {}) (N int, err error)
Println writes all parameters to the standard output in the default format and adds a line break at the end. If both adjacent parameters are not strings, a blank space is added between parameters. The function returns the number of bytes written and any errors encountered.
Func Scan
Func scan (a... interface {}) (N int, err error)
Scan reads text from the standard input and stores the consecutive data separated by spaces in the parameters. Line feed is regarded as blank. It returns the number of successfully read parameters. If the number is less than the number of provided parameters, the returned value err will report the cause.
Func scanf
Func scanf (Format String, A... interface {}) (N int, err error)
Scanf reads text from the standard input and stores the data in parameters according to the format string sequence. It returns the number of successfully parsed and saved parameters.
Func scanln
Func scanln (a... interface {}) (N int, err error)
Scanln is similar to scan, but it will be aborted when the line break is saved to the last line, and the reading position must have a line break or end character.
Func Sprint
Func Sprint (a... interface {}) String
Sprint writes all parameters in the default format and generates a string. If neither of the adjacent parameters is a string, a blank space is added between the parameters.
Func sprintf
Func sprintf (Format String, A... interface {}) String
Sprintf writes parameters according to the format string and returns the generated string.
Func sprintln
Func sprintln (a... interface {}) String
Sprintln writes all parameters in the default format and generates a string. If neither of the adjacent parameters is a string, a blank space is added between the parameters. The linefeed is added at the end of the string.
Func sscan
Func sscan (STR string, A... interface {}) (N int, err error)
Sscan reads text from a string, and stores the white-space split continuous data in the parameter sequence. Line feed is regarded as blank. It returns the number of successfully read parameters. If the number is less than the number of provided parameters, the returned value err will report the cause.
Func sscanf
Func sscanf (STR string, Format String, A... interface {}) (N int, err error)
Scanf reads text from a string and stores data in parameters according to the format string sequence. It returns the number of successfully parsed and saved parameters.
Func sscanln
Func sscanln (STR string, A... interface {}) (N int, err error)
Sscanln is similar to sscan, but it is aborted with a line break. When the last line is saved, the reading position must have a line break or an ending character.
Type formatter
Type formatter interface {
Format (F state, C Rune)
}
Formatter is a custom formatter interface. To implement the format method, you may need to call functions such as sprintf or fprintf (f) to generate the output.
Type gostringer
Type gostringer interface {
Gostring () String
}
The gostringer interface is implemented by any data that contains the gostring method. This method defines the go syntax format of the data. The gostring method is used to output values when the % # V format flag is used.
Type scanstate
Type scanstate interface {
// The readrune function reads the next Unicode symbol from the input. If it is called in scanln, fscanln, or sscanln, this function returns EOF when the first '\ n' is read or the maximum width is reached.
Readrune () (R Rune, size int, err error)
// Unreadrune returns the same character for the next call of readrune.
Unreadrune () Error
// Skipspace skips the input blank. Line feed is considered as blank (except scanln, fscanln, and sscanln, and linefeeds in these three functions are considered as EOF ).
Skipspace ()
// The token method skips the white space in the input when skipspace is true, and returns a Unicode character that meets F (c. If F is nil, use it! Unicode. isspace (C) (that is, the first non-space UNICODE character is returned );
// This function is valid only for non-null characters. Line breaks are considered as blank characters (except scanln, fscanln, and sscanln, and linefeeds in these three functions are considered as EOF ).
// The returned slice type points to the shared data, which can be overwritten by the next token call (using the scanstate interface as the input to call the scan function) or when the returned scan method is called.
Token (skipspace bool, F func (Rune) bool) (token [] Byte, err error)
// Width: returns the value of the width option and whether it is set.
Width () (WID int, OK bool)
// Because readrune is implemented using interfaces, the read method should never be called by the scan program. A good scanstate implementation should always return errors from read.
Read (BUF [] Byte) (N int, err error)
}
Scanstate is an interface that provides parameters for custom callback interfaces. The token interface may scan one character at a time or require scanstate to detect the next blank token.
Type Encoding
Type multicast interface {
Scan (State scanstate, verb Rune) Error
}
Any object that implements the scan method implements the dig interface. The scan method reads data from the input and saves the processing result to the receiver. the receiver must be a valid pointer. The scan method is called by any functions such as scan, scanf, and scanln, as long as the corresponding parameters implement this method.
Type state
Type state interface {
// Write can be called to produce formatted output.
Write (B [] Byte) (Ret int, err error)
// Width: returns the value of the width and whether it is set.
Width () (WID int, OK bool)
// The value of precision returned and whether or not it is set.
Precision () (Prec int, OK bool)
// Flag returns the symbol (plus or minus sign ......) Is the value set.
Flag (c int) bool
}
State is an interface that provides output parameters to the formatter interface. It provides access to the IO. Writer interface and formatted parameters using the provided data.
Type stringer
Type Stringer interface {
String () String
}
The stringer interface is automatically implemented by any type that implements the string method. This method defines the "native" format of this type. The string method is used to output parameters. When % s or % v format is used, or is output by a function that does not use the format string, such as print.