{"id":3747,"date":"2024-02-22T18:01:01","date_gmt":"2024-02-22T10:01:01","guid":{"rendered":""},"modified":"2024-02-22T18:01:01","modified_gmt":"2024-02-22T10:01:01","slug":"Go\u8bed\u8a00\u5b66\u4e60 fmt\u5305-\u662f\u4ec0\u4e48\u610f\u601d","status":"publish","type":"post","link":"https:\/\/mushiming.com\/3747.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60 fmt\u5305-\u662f\u4ec0\u4e48\u610f\u601d"},"content":{"rendered":"
fmt \u5305\u7684\u542b\u4e49\u662f\u4ec0\u4e48\uff1f<\/p>\n
\u5f00\u59cb\u5b66\u4e60go\u8bed\u8a00\uff0c\u5c31\u662ffmt\u7684\u5305\uff0c\u8fd9\u4e2a\u5305\u4e3a\u5565\u53ebfmt\u5305\u5462\uff1f\u53ef\u4ee5\u53c2\u8003 go\u8bed\u8a00\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n
\u53c2\u8003\uff1afmt\/doc.go \uff08\u4e0b\u9762\u4ee3\u7801\u6240\u793a\uff09<\/p>\n
go\u8bed\u8a00\u7684fmt package \uff0c\u5b9e\u73b0\u4e86\u540cC\u8bed\u8a00\u7684printf\uff0cscanf\u7c7b\u4f3c formatted I\/O \u7684functions\u3002<\/p>\n
\u8fd9\u4e2afomat \u52a8\u8bcd\u884d\u751f\u4e8eC\u8bed\u8a00\uff0c\u4f46\u662f\u76f8\u6bd4\u8f83\u66f4\u52a0\u7b80\u5355\u3002<\/p>\n
go doc fmt<\/code><\/pre>\n\n 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.<\/p>\n<\/blockquote>\n
\/\/ Copyright 2009 The Go Authors. All rights reserved.\n\/\/ Use of this source code is governed by a BSD-style\n\/\/ license that can be found in the LICENSE file.\n\n\/*\n\tPackage fmt implements formatted I\/O with functions analogous\n\tto C's printf and scanf. The format 'verbs' are derived from C's but\n\tare simpler.\n\n\n\tPrinting\n\n\tThe verbs:\n\n\tGeneral:\n\t\t%v\tthe value in a default format\n\t\t\twhen printing structs, the plus flag (%+v) adds field names\n\t\t%#v\ta Go-syntax representation of the value\n\t\t%T\ta Go-syntax representation of the type of the value\n\t\t%%\ta literal percent sign; consumes no value\n\n\tBoolean:\n\t\t%t\tthe word true or false\n\tInteger:\n\t\t%b\tbase 2\n\t\t%c\tthe character represented by the corresponding Unicode code point\n\t\t%d\tbase 10\n\t\t%o\tbase 8\n\t\t%O\tbase 8 with 0o prefix\n\t\t%q\ta single-quoted character literal safely escaped with Go syntax.\n\t\t%x\tbase 16, with lower-case letters for a-f\n\t\t%X\tbase 16, with upper-case letters for A-F\n\t\t%U\tUnicode format: U+1234; same as \"U+%04X\"\n\tFloating-point and complex constituents:\n\t\t%b\tdecimalless scientific notation with exponent a power of two,\n\t\t\tin the manner of strconv.FormatFloat with the 'b' format,\n\t\t\te.g. -123456p-78\n\t\t%e\tscientific notation, e.g. -1.234456e+78\n\t\t%E\tscientific notation, e.g. -1.234456E+78\n\t\t%f\tdecimal point but no exponent, e.g. 123.456\n\t\t%F\tsynonym for %f\n\t\t%g\t%e for large exponents, %f otherwise. Precision is discussed below.\n\t\t%G\t%E for large exponents, %F otherwise\n\t\t%x\thexadecimal notation (with decimal power of two exponent), e.g. -0x1.23abcp+20\n\t\t%X\tupper-case hexadecimal notation, e.g. -0X1.23ABCP+20\n\tString and slice of bytes (treated equivalently with these verbs):\n\t\t%s\tthe uninterpreted bytes of the string or slice\n\t\t%q\ta double-quoted string safely escaped with Go syntax\n\t\t%x\tbase 16, lower-case, two characters per byte\n\t\t%X\tbase 16, upper-case, two characters per byte\n\tSlice:\n\t\t%p\taddress of 0th element in base 16 notation, with leading 0x\n\tPointer:\n\t\t%p\tbase 16 notation, with leading 0x\n\t\tThe %b, %d, %o, %x and %X verbs also work with pointers,\n\t\tformatting the value exactly as if it were an integer.\n\n\tThe default format for %v is:\n\t\tbool: %t\n\t\tint, int8 etc.: %d\n\t\tuint, uint8 etc.: %d, %#x if printed with %#v\n\t\tfloat32, complex64, etc: %g\n\t\tstring: %s\n\t\tchan: %p\n\t\tpointer: %p\n\tFor compound objects, the elements are printed using these rules, recursively,\n\tlaid out like this:\n\t\tstruct: {field0 field1 ...}\n\t\tarray, slice: [elem0 elem1 ...]\n\t\tmaps: map[key1:value1 key2:value2 ...]\n\t\tpointer to above: &{}, &[], &map[]\n\n\tWidth is specified by an optional decimal number immediately preceding the verb.\n\tIf absent, the width is whatever is necessary to represent the value.\n\tPrecision is specified after the (optional) width by a period followed by a\n\tdecimal number. If no period is present, a default precision is used.\n\tA period with no following number specifies a precision of zero.\n\tExamples:\n\t\t%f default width, default precision\n\t\t%9f width 9, default precision\n\t\t%.2f default width, precision 2\n\t\t%9.2f width 9, precision 2\n\t\t%9.f width 9, precision 0\n\n\tWidth and precision are measured in units of Unicode code points,\n\tthat is, runes. (This differs from C's printf where the\n\tunits are always measured in bytes.) Either or both of the flags\n\tmay be replaced with the character '*', causing their values to be\n\tobtained from the next operand (preceding the one to format),\n\twhich must be of type int.\n\n\tFor most values, width is the minimum number of runes to output,\n\tpadding the formatted form with spaces if necessary.\n\n\tFor strings, byte slices and byte arrays, however, precision\n\tlimits the length of the input to be formatted (not the size of\n\tthe output), truncating if necessary. Normally it is measured in\n\trunes, but for these types when formatted with the %x or %X format\n\tit is measured in bytes.\n\n\tFor floating-point values, width sets the minimum width of the field and\n\tprecision sets the number of places after the decimal, if appropriate,\n\texcept that for %g\/%G precision sets the maximum number of significant\n\tdigits (trailing zeros are removed). For example, given 12.345 the format\n\t%6.3f prints 12.345 while %.3g prints 12.3. The default precision for %e, %f\n\tand %#g is 6; for %g it is the smallest number of digits necessary to identify\n\tthe value uniquely.\n\n\tFor complex numbers, the width and precision apply to the two\n\tcomponents independently and the result is parenthesized, so %f applied\n\tto 1.2+3.4i produces (1.200000+3.400000i).\n\n\tOther flags:\n\t\t+\talways print a sign for numeric values;\n\t\t\tguarantee ASCII-only output for %q (%+q)\n\t\t-\tpad with spaces on the right rather than the left (left-justify the field)\n\t\t#\talternate format: add leading 0b for binary (%#b), 0 for octal (%#o),\n\t\t\t0x or 0X for hex (%#x or %#X); suppress 0x for %p (%#p);\n\t\t\tfor %q, print a raw (backquoted) string if strconv.CanBackquote\n\t\t\treturns true;\n\t\t\talways print a decimal point for %e, %E, %f, %F, %g and %G;\n\t\t\tdo not remove trailing zeros for %g and %G;\n\t\t\twrite e.g. U+0078 'x' if the character is printable for %U (%#U).\n\t\t' '\t(space) leave a space for elided sign in numbers (% d);\n\t\t\tput spaces between bytes printing strings or slices in hex (% x, % X)\n\t\t0\tpad with leading zeros rather than spaces;\n\t\t\tfor numbers, this moves the padding after the sign\n\n\tFlags are ignored by verbs that do not expect them.\n\tFor example there is no alternate decimal format, so %#d and %d\n\tbehave identically.\n\n\tFor each Printf-like function, there is also a Print function\n\tthat takes no format and is equivalent to saying %v for every\n\toperand. Another variant Println inserts blanks between\n\toperands and appends a newline.\n\n\tRegardless of the verb, if an operand is an interface value,\n\tthe internal concrete value is used, not the interface itself.\n\tThus:\n\t\tvar i interface{} = 23\n\t\tfmt.Printf(\"%v\\n\", i)\n\twill print 23.\n\n\tExcept when printed using the verbs %T and %p, special\n\tformatting considerations apply for operands that implement\n\tcertain interfaces. In order of application:\n\n\t1. If the operand is a reflect.Value, the operand is replaced by the\n\tconcrete value that it holds, and printing continues with the next rule.\n\n\t2. If an operand implements the Formatter interface, it will\n\tbe invoked. In this case the interpretation of verbs and flags is\n\tcontrolled by that implementation.\n\n\t3. If the %v verb is used with the # flag (%#v) and the operand\n\timplements the GoStringer interface, that will be invoked.\n\n\tIf the format (which is implicitly %v for Println etc.) is valid\n\tfor a string (%s %q %v %x %X), the following two rules apply:\n\n\t4. If an operand implements the error interface, the Error method\n\twill be invoked to convert the object to a string, which will then\n\tbe formatted as required by the verb (if any).\n\n\t5. If an operand implements method String() string, that method\n\twill be invoked to convert the object to a string, which will then\n\tbe formatted as required by the verb (if any).\n\n\tFor compound operands such as slices and structs, the format\n\tapplies to the elements of each operand, recursively, not to the\n\toperand as a whole. Thus %q will quote each element of a slice\n\tof strings, and %6.2f will control formatting for each element\n\tof a floating-point array.\n\n\tHowever, when printing a byte slice with a string-like verb\n\t(%s %q %x %X), it is treated identically to a string, as a single item.\n\n\tTo avoid recursion in cases such as\n\t\ttype X string\n\t\tfunc (x X) String() string { return Sprintf(\"<%s>\", x) }\n\tconvert the value before recurring:\n\t\tfunc (x X) String() string { return Sprintf(\"<%s>\", string(x)) }\n\tInfinite recursion can also be triggered by self-referential data\n\tstructures, such as a slice that contains itself as an element, if\n\tthat type has a String method. Such pathologies are rare, however,\n\tand the package does not protect against them.\n\n\tWhen printing a struct, fmt cannot and therefore does not invoke\n\tformatting methods such as Error or String on unexported fields.\n\n\tExplicit argument indexes\n\n\tIn Printf, Sprintf, and Fprintf, the default behavior is for each\n\tformatting verb to format successive arguments passed in the call.\n\tHowever, the notation [n] immediately before the verb indicates that the\n\tnth one-indexed argument is to be formatted instead. The same notation\n\tbefore a '*' for a width or precision selects the argument index holding\n\tthe value. After processing a bracketed expression [n], subsequent verbs\n\twill use arguments n+1, n+2, etc. unless otherwise directed.\n\n\tFor example,\n\t\tfmt.Sprintf(\"%[2]d %[1]d\\n\", 11, 22)\n\twill yield \"22 11\", while\n\t\tfmt.Sprintf(\"%[3]*.[2]*[1]f\", 12.0, 2, 6)\n\tequivalent to\n\t\tfmt.Sprintf(\"%6.2f\", 12.0)\n\twill yield \" 12.00\". Because an explicit index affects subsequent verbs,\n\tthis notation can be used to print the same values multiple times\n\tby resetting the index for the first argument to be repeated:\n\t\tfmt.Sprintf(\"%d %d %#[1]x %#x\", 16, 17)\n\twill yield \"16 17 0x10 0x11\".\n\n\tFormat errors\n\n\tIf an invalid argument is given for a verb, such as providing\n\ta string to %d, the generated string will contain a\n\tdescription of the problem, as in these examples:\n\n\t\tWrong type or unknown verb: %!verb(type=value)\n\t\t\tPrintf(\"%d\", \"hi\"): %!d(string=hi)\n\t\tToo many arguments: %!(EXTRA type=value)\n\t\t\tPrintf(\"hi\", \"guys\"): hi%!(EXTRA string=guys)\n\t\tToo few arguments: %!verb(MISSING)\n\t\t\tPrintf(\"hi%d\"): hi%!d(MISSING)\n\t\tNon-int for width or precision: %!(BADWIDTH) or %!(BADPREC)\n\t\t\tPrintf(\"%*s\", 4.5, \"hi\"): %!(BADWIDTH)hi\n\t\t\tPrintf(\"%.*s\", 4.5, \"hi\"): %!(BADPREC)hi\n\t\tInvalid or invalid use of argument index: %!(BADINDEX)\n\t\t\tPrintf(\"%*[2]d\", 7): %!d(BADINDEX)\n\t\t\tPrintf(\"%.[2]d\", 7): %!d(BADINDEX)\n\n\tAll errors begin with the string \"%!\" followed sometimes\n\tby a single character (the verb) and end with a parenthesized\n\tdescription.\n\n\tIf an Error or String method triggers a panic when called by a\n\tprint routine, the fmt package reformats the error message\n\tfrom the panic, decorating it with an indication that it came\n\tthrough the fmt package. For example, if a String method\n\tcalls panic(\"bad\"), the resulting formatted message will look\n\tlike\n\t\t%!s(PANIC=bad)\n\n\tThe %!s just shows the print verb in use when the failure\n\toccurred. If the panic is caused by a nil receiver to an Error\n\tor String method, however, the output is the undecorated\n\tstring, \"<nil>\".\n\n\tScanning\n\n\tAn analogous set of functions scans formatted text to yield\n\tvalues. Scan, Scanf and Scanln read from os.Stdin; Fscan,\n\tFscanf and Fscanln read from a specified io.Reader; Sscan,\n\tSscanf and Sscanln read from an argument string.\n\n\tScan, Fscan, Sscan treat newlines in the input as spaces.\n\n\tScanln, Fscanln and Sscanln stop scanning at a newline and\n\trequire that the items be followed by a newline or EOF.\n\n\tScanf, Fscanf, and Sscanf parse the arguments according to a\n\tformat string, analogous to that of Printf. In the text that\n\tfollows, 'space' means any Unicode whitespace character\n\texcept newline.\n\n\tIn the format string, a verb introduced by the % character\n\tconsumes and parses input; these verbs are described in more\n\tdetail below. A character other than %, space, or newline in\n\tthe format consumes exactly that input character, which must\n\tbe present. A newline with zero or more spaces before it in\n\tthe format string consumes zero or more spaces in the input\n\tfollowed by a single newline or the end of the input. A space\n\tfollowing a newline in the format string consumes zero or more\n\tspaces in the input. Otherwise, any run of one or more spaces\n\tin the format string consumes as many spaces as possible in\n\tthe input. Unless the run of spaces in the format string\n\tappears adjacent to a newline, the run must consume at least\n\tone space from the input or find the end of the input.\n\n\tThe handling of spaces and newlines differs from that of C's\n\tscanf family: in C, newlines are treated as any other space,\n\tand it is never an error when a run of spaces in the format\n\tstring finds no spaces to consume in the input.\n\n\tThe verbs behave analogously to those of Printf.\n\tFor example, %x will scan an integer as a hexadecimal number,\n\tand %v will scan the default representation format for the value.\n\tThe Printf verbs %p and %T and the flags # and + are not implemented.\n\tFor floating-point and complex values, all valid formatting verbs\n\t(%b %e %E %f %F %g %G %x %X and %v) are equivalent and accept\n\tboth decimal and hexadecimal notation (for example: \"2.3e+7\", \"0x4.5p-8\")\n\tand digit-separating underscores (for example: \"3.14159_26535_89793\").\n\n\tInput processed by verbs is implicitly space-delimited: the\n\timplementation of every verb except %c starts by discarding\n\tleading spaces from the remaining input, and the %s verb\n\t(and %v reading into a string) stops consuming input at the first\n\tspace or newline character.\n\n\tThe familiar base-setting prefixes 0b (binary), 0o and 0 (octal),\n\tand 0x (hexadecimal) are accepted when scanning integers\n\twithout a format or with the %v verb, as are digit-separating\n\tunderscores.\n\n\tWidth is interpreted in the input text but there is no\n\tsyntax for scanning with a precision (no %5.2f, just %5f).\n\tIf width is provided, it applies after leading spaces are\n\ttrimmed and specifies the maximum number of runes to read\n\tto satisfy the verb. For example,\n\t Sscanf(\" 1234567 \", \"%5s%d\", &s, &i)\n\twill set s to \"12345\" and i to 67 while\n\t Sscanf(\" 12 34 567 \", \"%5s%d\", &s, &i)\n\twill set s to \"12\" and i to 34.\n\n\tIn all the scanning functions, a carriage return followed\n\timmediately by a newline is treated as a plain newline\n\t(\\r\\n means the same as \\n).\n\n\tIn all the scanning functions, if an operand implements method\n\tScan (that is, it implements the Scanner interface) that\n\tmethod will be used to scan the text for that operand. Also,\n\tif the number of arguments scanned is less than the number of\n\targuments provided, an error is returned.\n\n\tAll arguments to be scanned must be either pointers to basic\n\ttypes or implementations of the Scanner interface.\n\n\tLike Scanf and Fscanf, Sscanf need not consume its entire input.\n\tThere is no way to recover how much of the input string Sscanf used.\n\n\tNote: Fscan etc. can read one character (rune) past the input\n\tthey return, which means that a loop calling a scan routine\n\tmay skip some of the input. This is usually a problem only\n\twhen there is no space between input values. If the reader\n\tprovided to Fscan implements ReadRune, that method will be used\n\tto read characters. If the reader also implements UnreadRune,\n\tthat method will be used to save the character and successive\n\tcalls will not lose data. To attach ReadRune and UnreadRune\n\tmethods to a reader without that capability, use\n\tbufio.NewReader.\n*\/\npackage fmt\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60 fmt\u5305-\u662f\u4ec0\u4e48\u610f\u601dfmt\u5305\u7684\u542b\u4e49\u662f\u4ec0\u4e48\uff1f\u5f00\u59cb\u5b66\u4e60go\u8bed\u8a00\uff0c\u5c31\u662ffmt\u7684\u5305\uff0c\u8fd9\u4e2a\u5305\u4e3a\u5565\u53ebfmt\u5305\u5462\uff1f\u53ef\u4ee5\u53c2\u8003go\u8bed\u8a00\u7684\u6587\u6863","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/3747"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=3747"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/3747\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=3747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=3747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=3747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}