fmt
36 分钟阅读
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.
fmt包实现了类似于C的printf和scanf的格式化I/O输入Input 和输出 Ouput)功能。格式化的"verbs(动词)“来自于C,但更简单。
打印 Printing
动词 Verbs
通用 General
%v 以默认格式打印值
当打印结构体时,加号(%+v)会增加字段名。
%#v 值的Go语法表示
%T 值的类型的Go语法表示
%% 一个字面百分号;不消耗任何值
布尔 Boolean
%t 单词true或false
整数 Integer
%b 二进制
%c 对应的Unicode码点所表示的字符
%d 十进制
%o 八进制
%O 带有0o前缀的八进制
%q 使用Go语法安全转义的单引号字符字面量。
%x 十六进制,小写字母a-f
%X 十六进制,大写字母A-F
%U Unicode格式:U+1234;与"U+%04X"相同
浮点数和复数成分 Floating-point and complex constituents
%b 指数为二的幂的无小数科学计数法,
类似于strconv.FormatFloat的'b'格式,例如-123456p-78
%e 科学计数法,例如-1.234456e+78
%E 科学计数法,例如-1.234456E+78
%f 有小数点但没有指数,例如123.456
%F %f的同义词
%g 大指数的%e,否则是%f。精度将在下面讨论。
%G 大指数的%E,否则是%F
%x 十六进制(带有两个十进制指数幂),例如-0x1.23abcp+20
%X 大写版%x,例如:-0X1.23ABCP+20
字符串和字节切片(使用这些动词等效) String and slice of bytes (treated equivalently with these verbs)
%s 字符串或切片的未解释字节
%q 使用Go语法安全转义的双引号字符串
(
%q(引用字符串)格式指令用于以Go语言的双引号形式输出字符串,其中会直接将可打印字符的可打印字面量输出,而其他不可打印字符则使用转义的形式输出。
如果使用了+号修饰符,那么只有ASCII字符(从、U+0020到U+007E)会直接输出,而其他字符则以转义字符形式输出。
如果使用了#修饰符,那么只要在可能的情况下就会输出Go原始字符串,否则输出以双引号引用的字符串。
摘自《Go语言程序设计 - Mark Summerfield(英)著》
)
s := "End Ó ré ttlæti♥中国,世界"
fmt.Printf("%s\n", s) // End Ó ré ttlæti♥中国,世界
fmt.Printf("%q\n", s) // "End Ó ré ttlæti♥中国,世界"
fmt.Printf("%+q\n", s) // "End \u00d3 r\u00e9 ttl\u00e6ti\u2665\u4e2d\u56fd\uff0c\u4e16\u754c"
fmt.Printf("%#q\n", s) // `End Ó ré ttlæti♥中国,世界`
%x 十六进制,小写字母,每个字节两个字符
%X 十六进制,大写字母,每个字节两个字符
切片 Slice
%p 以基数16表示的第0个元素的地址,带有前导0x
指针 Pointer
%p 以基数16表示,带有前导0x
%b、%d、%o、%x和%X动词也适用于指针,将该值格式化为整数
%v的默认格式 The default format for %v is
bool: %t
int, int8 etc.: %d
uint, uint8 etc.: %d,使用%#v打印时为%#x
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:
宽度(Width)是在动词之前的可选十进制数指定的。如果没有指定,则宽度为表示该值所需的任何宽度。精度(Precision )是在(可选的)宽度之后由一个句点后跟一个十进制数指定的。如果没有句号,则使用默认精度。没有后面的数字的句号指定精度为零。例如:
%f 默认宽度,默认精度
%9f 宽度9,默认精度
%.2f 默认宽度,精度为2
%9.2f 宽度9,精度为2
%9.f 宽度9,精度为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.
宽度和精度以Unicode码点为单位衡量,即符文。 (这与C的printf不同,其中单位始终以字节为单位衡量。)其中一个或两个标志可以用字符’*
‘替换,从而导致它们的值从要格式化的下一个操作数(必须是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.
对于字符串,字节切片和字节数组,精度限制要格式化的输入的长度(而不是输出的大小),必要时截断。通常,它以符文为单位衡量,但对于这些类型,如果使用%x
或%X
格式进行格式化,则以字节为单位衡量。
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.
对于浮点值,宽度设置字段的最小宽度,精度设置小数点后的位数(如果适用),但是对于%g
或 %G
,精度设置最大有效数字的数量(尾随零被删除)。例如,给定12.345,格式%6.3f
打印12.345,而%.3g
打印12.3。%e
,%f
和%#g
的默认精度为6;对于%g
,它是唯一确定该值的最小数字数。(不好理解,见以下例子)
|
|
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).
对于复数,宽度和精度分别应用于两个组成部分,结果用圆括号括起来的,所以%f
应用于1.2+3.4i
将产生(1.200000+3.400000i)
。
When formatting a single integer code point or a rune string (type []rune) with %q, invalid Unicode code points are changed to the Unicode replacement character, U+FFFD, as in strconv.QuoteRune.
使用%q
格式化单个整数码点或符文字符串(类型[] rune
)时,无效的Unicode码点将更改为Unicode替换字符U+FFFD
,如strconv.QuoteRune中所述。(不好理解,见以下例子)
|
|
输出:
'中' - U+4E2D
'国' - U+56FD
'�' - U+FFFD
其他标志 Other flags
'+' 总是为数字值打印一个标记。
保证在 %q 中输出 ASCII-only(使用 %+q)
(
让格式指令在数值前面输出+号或者-号,为字符串输出ASCII字符(别的字符会被转义),为结构体输出其字段名字
摘自《Go语言程序设计 - Mark Summerfield(英)著》
)
'-' 在右侧填充空格而不是左侧(左对齐字段)
'#' 替代格式:
在二进制(%#b)前添加前导的 0b,
在八进制(%#o)前添加 0,
在十六进制(%#x 或 %#X)前添加 0x 或 0X;
对于 %q,如果 strconv.CanBackquote 返回 true,
则打印原始的(反引号括起来的)字符串;
对于 %e、%E、%f、%F、%g 和 %G,总是打印小数点;
对于 %g 和 %G,不要移除尾随的零;
对于 %U,如果字符可打印,则打印类似 U+0078 'x' 的形式(%#U)。
(
%#o输出以0打头的八进制数据;
%#p输出不含0x打头的指针;
%#q尽可能以原始字符串的形式输出一个字符串或者[]byte切片(使用反引号),否则输出以双引号引起来的字符串。
摘自《Go语言程序设计 - Mark Summerfield(英)著》
)
' ' (space) leave a space for elided sign in numbers (% d);
put spaces between bytes printing strings or slices in hex (% x, % X)
=> (空格)为数字中的省略标记留一个空格(% d)。
在字节之间留出空格,打印字符串或十六进制的切片(% x, % X)
(空格)在数字中留出一个空格用于省略的符号(% d);
在以十六进制形式(% x、% X)打印字符串或字节片时,将字节之间放置空格。
=> fmt.Printf("数字以%% d格式打印后为% d值\n", 123456789)
//数字以% d格式打印后为 123456789值
=> fmt.Printf("数字以%%d格式打印后为%d值\n", 123456789)
//数字以%d格式打印后为123456789值
=> fmt.Printf("% x\n", "hello world")
//68 65 6c 6c 6f 20 77 6f 72 6c 64
=> fmt.Printf("%x\n", "hello world")
//68656c6c6f20776f726c64
=> fmt.Printf("% x\n", []int{0x12, 0x13, 0x14})
//[ 12 13 14]
=> fmt.Printf("%x\n", []int{0x12, 0x13, 0x14})
//[12 13 14]
'0' 在前导位置填充零而不是空格;
对于数字,这将在符号后面移动填充;
对于字符串、字节片和字节数组,将被忽略。
f1 := -1.235
f2 := 1.235
fmt.Printf("%+09.3f\n", f1) // -0001.235
fmt.Printf("%+09.3f\n", f2) // +0001.235
Flags are ignored by verbs that do not expect them. For example there is no alternate decimal format, so %#d and %d behave identically.
对于不带标志的动词,标志将被忽略。例如,没有替代的十进制格式,因此 %#d 和 %d 的行为相同。
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.
对于每个类似 Printf 的函数,也有一个不带格式的 Print 函数,它相当于为每个操作数都使用 %v。另一种变体 Println 在操作数之间插入空格并追加一个换行符。
Regardless of the verb, if an operand is an interface value, the internal concrete value is used, not the interface itself. Thus:
无论动词如何,如果操作数是接口值,则使用内部具体值,而不是接口本身。因此:
|
|
will print 23.
将打印23。
Except when printed using the verbs %T and %p, special formatting considerations apply for operands that implement certain interfaces. In order of application:
除非使用%T和%p这两个格式,否则针对实现特定接口的操作数,会有特殊的格式化考虑。按应用顺序分别为:
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.
如果操作数是reflect.Value,则将其替换为其持有的具体值,并继续打印下一个规则。
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.
如果操作数实现了Formatter接口,则将调用该接口。在这种情况下,动词和标志的解释由该实现控制。
If the %v verb is used with the # flag (%#v) and the operand implements the GoStringer interface, that will be invoked.
如果使用带有#标志(%#v)的%v动词并且操作数实现了GoStringer接口,则将调用它。
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:
如果格式(在Println等中隐式为%v)对于字符串(%s %q %v %x %X)有效,则应用以下两个规则:
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).
如果操作数实现了错误接口,则将调用Error方法将对象转换为字符串,然后按照动词(如果有)的要求进行格式化。
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).
如果操作数实现了方法String() string,则将调用该方法将对象转换为字符串,然后按照动词(如果有)的要求进行格式化。
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.
对于像切片和结构体这样的复合操作数,格式递归地应用于每个操作数的元素,而不是整个操作数。因此,%q将引用字符串切片的每个元素,%6.2f将控制浮点数数组的每个元素的格式。
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.
但是,当使用类似字符串的动词(%s %q %x %X)打印字节切片时,它与字符串一样被视为单个项。
To avoid recursion in cases such as
为避免出现递归,例如
|
|
convert the value before recurring:
在递归之前将值转换:
|
|
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.
如果这种类型有String方法,则还可以触发自引用数据结构(例如包含自身作为元素的切片)的无限递归。然而,这种病理情况很少见,而且该包不会保护它们。
When printing a struct, fmt cannot and therefore does not invoke formatting methods such as Error or String on unexported fields.
在打印结构体时,fmt不能且因此不会调用未导出字段的格式化方法,例如Error或String。
显式参数索引 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.
在Printf、Sprintf和Fprintf中,默认行为是为每个格式化动词格式化在调用中传递的连续参数。但是,在动词之前的[n]表示将格式化第n个一索引参数。在’*
‘前面的相同符号表示选择包含该值的参数索引。在处理括号表达式[n]后,后续的动词将使用n + 1、n + 2等参数,除非另有指示。
For example,
例如,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield “22 11”, while
将生成 “22 11”,而
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:
将产生" 12.00"。因为显式索引会影响后续动词,所以可以使用此符号将第一个要重复的参数的索引重置为多次打印相同的值:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield “16 17 0x10 0x11”.
将生成 “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:
如果对动词提供了无效的参数,例如在%d
中提供了字符串,则生成的字符串将包含问题的描述,例如:
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
如果一个Error或String方法在被打印例程调用时触发了panic,fmt包将重新格式化来自panic的错误消息,用指示它来自fmt包的标识进行修饰。例如,如果String方法调用panic(“bad”),则生成的格式化消息将如下所示
%!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, “
%!s
只是显示故障发生时使用的打印动词。然而,如果panic是由于Error或String方法的nil接收器引起的,则输出为未装饰的字符串"<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、Scanf 和 Scanln 从 os.Stdin 读取;Fscan、Fscanf 和 Fscanln 从指定的 io.Reader 读取;Sscan、Sscanf 和 Sscanln 从参数字符串中读取。
Scan, Fscan, Sscan treat newlines in the input as spaces.
Scan、Fscan、Sscan 将输入中的换行符视为空格。
Scanln, Fscanln and Sscanln stop scanning at a newline and require that the items be followed by a newline or EOF.
Scanln、Fscanln 和 Sscanln 在遇到换行符时停止扫描,并要求项后面跟随一个换行符或 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.
Scanf、Fscanf 和 Sscanf 根据格式字符串解析参数,类似于 Printf。在接下来的文本中,“空格"表示除换行符以外的任何 Unicode 空白字符。
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.
空格和换行符的处理与 C 的 scanf 家族不同:在 C 中,换行符被视为其他空格一样,当格式字符串中的空格序列在输入中找不到要消耗的空格时,这不是一个错误。
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”).
这些转换说明符的行为类似于 Printf 的行为。例如,%x 将十六进制数作为整数扫描,而 %v 将扫描值的默认表示格式。Printf 的说明符 %p 和 %T 以及标志 # 和 + 未实现。对于浮点数和复数值,所有有效的格式说明符(%b %e %E %f %F %g %G %x %X 和 %v)都是等效的,并且接受十进制和十六进制表示法(例如:“2.3e+7”、“0x4.5p-8”)和数字分隔符下划线(例如:“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.
转换说明符处理的输入隐式以空格为分隔符:每个说明符的实现除了 %c 开头会丢弃剩余输入中的前导空格,%s 说明符(以及 %v 读入字符串)在遇到第一个空格或换行符时停止消耗输入。
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.
在没有格式或使用%v动词的情况下扫描整数时,接受熟悉的基数设置前缀0b(二进制)、0o和0(八进制)和0x(十六进制),以及数字分隔下划线。
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,
宽度在输入文本中解释,但没有用于扫描精度的语法(没有%5.2f,只有%5f)。如果提供了宽度,则在修剪前导空格之后应用它,并指定满足动词所需读取的符文的最大数量。例如,
Sscanf(" 1234567 ", "%5s%d", &s, &i)
will set s to “12345” and i to 67 while
将s设置为"12345”,将i设置为67,而
Sscanf(" 12 34 567 ", "%5s%d", &s, &i)
will set s to “12” and i to 34.
将s设置为"12”,将i设置为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).
在所有扫描函数中,回车紧随换行符会被视为普通的换行符(\r\n和\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.
在所有扫描函数中,如果操作数实现了Scan方法(即实现了Scanner接口),则将使用该方法来扫描该操作数的文本。此外,如果扫描到的参数数量少于提供的参数数量,则会返回错误。
All arguments to be scanned must be either pointers to basic types or implementations of the Scanner interface.
所有要扫描的参数必须是基本类型的指针或Scanner接口的实现。
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.
与Scanf和Fscanf一样,Sscanf不需要消耗其整个输入。没有办法恢复Sscanf使用了多少输入字符串。
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.
注意:Fscan等函数可以读取它们返回的输入之后的一个字符(符文),这意味着调用扫描程序的循环可能会跳过部分输入。这通常仅在输入值之间没有空格时才会出现问题。如果提供给Fscan的读取器实现了ReadRune方法,则将使用该方法读取字符。如果读取器还实现了UnreadRune方法,则将使用该方法保存字符,并且连续的调用不会丢失数据。要将ReadRune和UnreadRune方法附加到没有该功能的读取器,请使用bufio.NewReader。
Example (Formats)
These examples demonstrate the basics of printing using a format string. Printf, Sprintf, and Fprintf all take a format string that specifies how to format the subsequent arguments. For example, %d (we call that a ‘verb’) says to print the corresponding argument, which must be an integer (or something containing an integer, such as a slice of ints) in decimal. The verb %v (‘v’ for ‘value’) always formats the argument in its default form, just how Print or Println would show it. The special verb %T (‘T’ for ‘Type’) prints the type of the argument rather than its value. The examples are not exhaustive; see the package comment for all the details.
这些示例演示了使用格式化字符串进行打印的基础知识。Printf、Sprintf和Fprintf都采用格式化字符串,指定如何格式化后续参数。例如,%d(我们称之为"动词")表示要打印相应的参数,该参数必须是十进制整数(或包含整数的内容,例如int切片)。动词%v(“v"表示"value”)始终以默认形式格式化参数,就像Print或Println显示它一样。特殊动词%T(“T"表示"Type”)打印参数的类型而不是其值。这些示例并不详尽,有关所有细节,请参见包的注释。
|
|
Example (Printers)
Print, Println, and Printf lay out their arguments differently. In this example we can compare their behaviors. Println always adds blanks between the items it prints, while Print adds blanks only between non-string arguments and Printf does exactly what it is told. Sprint, Sprintln, Sprintf, Fprint, Fprintln, and Fprintf behave the same as their corresponding Print, Println, and Printf functions shown here.
Print、Println和Printf在它们的参数布局方面有所不同。在这个示例中,我们可以比较它们的行为。Println始终在它打印的项之间添加空格,而Print仅在非字符串参数之间添加空格,并且Printf完全按照指令执行。Sprint、Sprintln、Sprintf、Fprint、Fprintln和Fprintf的行为与它们对应的Print、Println和Printf函数相同。
|
|
常量
This section is empty.
变量
This section is empty.
函数
func Append <- go1.19
|
|
Append formats using the default formats for its operands, appends the result to the byte slice, and returns the updated slice.
Append函数使用操作数的默认格式进行格式化,将结果附加到字节切片中,并返回更新后的切片。
Example My Append
|
|
func Appendf <- go1.19
|
|
Appendf formats according to a format specifier, appends the result to the byte slice, and returns the updated slice.
Appendf函数按照格式说明符进行格式化,将结果附加到字节切片中,并返回更新后的切片。
Example My Appendf
|
|
func Appendln <- go1.19
|
|
Appendln formats using the default formats for its operands, appends the result to the byte slice, and returns the updated slice. Spaces are always added between operands and a newline is appended.
Appendln函数使用操作数的默认格式进行格式化,将结果附加到字节切片中,并返回更新后的切片。在操作数之间始终添加空格,并附加一个换行符。
Example My Appendln
|
|
func Errorf
|
|
Errorf formats according to a format specifier and returns the string as a value that satisfies error.
Errorf函数按照格式说明符进行格式化,并将字符串作为满足error接口的值返回。
If the format specifier includes a %w verb with an error operand, the returned error will implement an Unwrap method returning the operand. If there is more than one %w verb, the returned error will implement an Unwrap method returning a []error containing all the %w operands in the order they appear in the arguments. It is invalid to supply the %w verb with an operand that does not implement the error interface. The %w verb is otherwise a synonym for %v.
如果格式说明符包含一个带有错误操作数的%w
动词,则返回的错误将实现一个返回该操作数的Unwrap方法。如果有多个%w
动词,则返回的错误将实现一个返回按出现在参数中的顺序包含所有%w
操作数的[]error类型的Unwrap方法。为%w
动词提供未实现错误接口的操作数是无效的。否则,%w
动词是%v
的同义词。
Errorf Example
The Errorf function lets us use formatting features to create descriptive error messages.
Errorf
函数允许我们使用格式化功能来创建描述性错误消息。
|
|
func FormatString <- go1.20
|
|
FormatString returns a string representing the fully qualified formatting directive captured by the State, followed by the argument verb. (State does not itself contain the verb.) The result has a leading percent sign followed by any flags, the width, and the precision. Missing flags, width, and precision are omitted. This function allows a Formatter to reconstruct the original directive triggering the call to Format.
FormatString函数返回一个字符串,表示由State捕获的完全限定的格式化指令,后跟操作数verb。(State本身不包含操作数。)结果具有一个前导百分号,后跟任何标志、宽度和精度。缺少的标志、宽度和精度将被省略。此函数允许Formatter重建触发调用Format的原始指令。
func Fprint
|
|
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.
Fprint函数使用操作数的默认格式进行格式化,并写入w中。当没有一个操作数是字符串时,它们之间添加空格。它返回写入的字节数和遇到的任何写入错误。
Fprint Example
|
|
func Fprintf
|
|
Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered.
Fprintf函数按照格式说明符对数据进行格式化,并将结果写入w。它返回写入的字节数和遇到的任何写入错误。
Fprintf Example
|
|
func Fprintln
|
|
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.
Fprintln函数按照默认格式对数据进行格式化,并将结果写入w。操作数之间总是添加空格,并追加一个换行符。它返回写入的字节数和遇到的任何写入错误。
Fprintln Example
|
|
func Fscan
|
|
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.
Fscan函数从r中读取文本,将连续的以空格分隔的值存储到连续的参数中。换行符也被视为空格。它返回成功扫描的条目数。如果返回值小于参数个数,则err报告失败的原因。
func Fscanf
|
|
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.
Fscanf函数从r中读取文本,按照格式说明符确定的方式,将连续的以空格分隔的值存储到连续的参数中。它返回成功解析的条目数。输入中的换行符必须与格式中的换行符匹配。
Fscanf Example
|
|
func Fscanln
|
|
Fscanln is similar to Fscan, but stops scanning at a newline and after the final item there must be a newline or EOF.
Fscanln函数类似于Fscan,但会在换行符处停止扫描,在最后一个项之后必须有一个换行符或EOF。
Fscanln Example
|
|
func Print
|
|
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.
Print函数按照默认格式对数据进行格式化,并写入标准输出。当两个操作数都不是字符串时,它们之间添加空格。它返回写入的字节数和遇到的任何写入错误。
Print Example
|
|
func Printf
|
|
Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.
Printf函数根据格式说明符格式化并写入标准输出。它返回写入的字节数和任何遇到的写入错误。
Printf Example
|
|
func Println
|
|
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.
Println函数根据操作数的默认格式进行格式化并写入标准输出。无论操作数是什么,都会添加空格,并追加一个换行符。它返回写入的字节数和任何遇到的写入错误。
Println Example
|
|
func Scan
|
|
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.
Scan函数扫描从标准输入
读取的文本,将连续的以空格分隔的值存储到连续的参数中。换行符会被视为空格。它返回成功扫描的项数。如果它小于参数个数,那么 err 将会报告原因。
func Scanf
|
|
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.
Scanf函数扫描从标准输入
读取的文本,根据format
将连续的以空格分隔的值存储到连续的参数中。它返回成功解析的项数。如果它小于参数个数,那么 err 将会报告原因。输入中的换行符必须与格式中的换行符相匹配。唯一的例外是,%c 动词总是扫描输入中的下一个符文,即使它是空格(或制表符等)或换行符。
func Scanln
|
|
Scanln is similar to Scan, but stops scanning at a newline and after the final item there must be a newline or EOF.
Scanln函数与 Scan函数类似,但会在换行符处停止扫描,在最后一项后必须有一个换行符或 EOF。
func Sprint
|
|
Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.
Sprint函数根据操作数的默认格式进行格式化,并返回生成的字符串。当两个操作数都不是字符串时,将在它们之间添加空格。
Sprint Example
|
|
func Sprintf
|
|
Sprintf formats according to a format specifier and returns the resulting string.
Sprintf函数根据格式说明符格式化并返回一个字符串。
Sprintf Example
|
|
func Sprintln
|
|
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.
Sprintln函数使用其操作数的默认格式进行格式化,并返回生成的字符串。在操作数之间始终添加空格,并附加换行符。
Sprintln Example
|
|
func Sscan
|
|
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.
Sscan函数扫描参数字符串
,将连续的以空格分隔的值存储到连续的参数中。换行符视为空格。它返回成功扫描的项数。如果此数小于参数个数,则err会报告原因。
func Sscanf
|
|
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.
Sscanf函数扫描参数字符串
,将根据格式将连续的以空格分隔的值存储到连续的参数中。它返回成功解析的项数。输入中的换行符必须与格式中的换行符匹配。
Sscanf Example
|
|
func Sscanln
|
|
Sscanln is similar to Sscan, but stops scanning at a newline and after the final item there must be a newline or EOF.
Sscanln函数类似于 Sscan函数,但在换行符处停止扫描,且最后一项后必须有一个换行符或 EOF。
类型
type Formatter
|
|
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.
Formatter 由任何具有 Format 方法的值实现。实现控制如何解释 State 和 rune,并可以调用 Sprint(f) 或 Fprint(f) 等来生成其输出。
type GoStringer
|
|
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.
GoStringer 由任何具有 GoString 方法的值实现,该方法定义该值的 Go 语法。GoString 方法用于打印作为 %#v 格式的操作数传递的值。
Example
|
|
type ScanState
|
|
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.
ScanState表示传递给自定义扫描器的扫描器状态。扫描器可以逐个rune扫描,也可以要求ScanState发现下一个以空格分隔的标记。
type Scanner
|
|
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.
Scanner由具有Scan方法的任何值实现,该方法扫描输入以查找值的表示,并将结果存储在接收器中,后者必须是指针才能有用。对于实现Scan方法的任何参数,都将调用Scan,Scanf或Scanln。
type State
|
|
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.
State表示传递给自定义格式化程序的打印机状态。它提供了对io.Writer接口的访问以及有关操作数格式说明符的标志和选项的信息。
type Stringer
|
|
Stringer is implemented by any value that has a String method, which defines the “native” 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.
Stringer由任何具有String方法的值实现,该方法定义该值的"原生"格式。String方法用于打印作为接受字符串的任何格式的操作数或打印机传递的未格式化操作数,例如Print。
Example
|
|